mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2025-12-24 18:20:07 +01:00
shared: suppress -Wstringop-truncation warning in nm_strndup_a()
The compiler is too smart for nm_strndup_a(). The code is correct, suppress "-Wstringop-truncation" warning.
This commit is contained in:
parent
d7d2dafc1b
commit
035c4ad45d
2 changed files with 61 additions and 1 deletions
|
|
@ -255,10 +255,12 @@ nm_memdup (gconstpointer data, gsize size)
|
|||
static inline char *
|
||||
_nm_strndup_a_step (char *s, const char *str, gsize len)
|
||||
{
|
||||
NM_PRAGMA_WARNING_DISABLE ("-Wstringop-truncation");
|
||||
if (len > 0)
|
||||
strncpy (s, str, len);
|
||||
s[len] = '\0';
|
||||
return s;
|
||||
NM_PRAGMA_WARNING_REENABLE;
|
||||
}
|
||||
|
||||
/* Similar to g_strndup(), however, if the string (including the terminating
|
||||
|
|
@ -269,7 +271,12 @@ _nm_strndup_a_step (char *s, const char *str, gsize len)
|
|||
*
|
||||
* In case malloc() is necessary, @out_str_free will be set (this string
|
||||
* must be freed afterwards). It is permissible to pass %NULL as @out_str_free,
|
||||
* if you ensure that len < alloca_maxlen. */
|
||||
* if you ensure that len < alloca_maxlen.
|
||||
*
|
||||
* Note that just like g_strndup(), this always returns a buffer with @len + 1
|
||||
* bytes, even if strlen(@str) is shorter than that (NUL terminated early). We fill
|
||||
* the buffer with strncpy(), which means, that @str is copied up to the first
|
||||
* NUL character and then filled with NUL characters. */
|
||||
#define nm_strndup_a(alloca_maxlen, str, len, out_str_free) \
|
||||
({ \
|
||||
const gsize _alloca_maxlen = (alloca_maxlen); \
|
||||
|
|
|
|||
|
|
@ -152,6 +152,58 @@ test_nm_strdup_int (void)
|
|||
|
||||
/*****************************************************************************/
|
||||
|
||||
static void
|
||||
test_nm_strndup_a (void)
|
||||
{
|
||||
int run;
|
||||
|
||||
for (run = 0; run < 20; run++) {
|
||||
gs_free char *input = NULL;
|
||||
char ch;
|
||||
gsize i, l;
|
||||
|
||||
input = g_strnfill (nmtst_get_rand_int () % 20, 'x');
|
||||
|
||||
for (i = 0; input[i]; i++) {
|
||||
while ((ch = ((char) nmtst_get_rand_int ())) == '\0') {
|
||||
/* repeat. */
|
||||
}
|
||||
input[i] = ch;
|
||||
}
|
||||
|
||||
{
|
||||
gs_free char *dup_free = NULL;
|
||||
const char *dup;
|
||||
|
||||
l = strlen (input) + 1;
|
||||
dup = nm_strndup_a (10, input, l - 1, &dup_free);
|
||||
g_assert_cmpstr (dup, ==, input);
|
||||
if (strlen (dup) < 10)
|
||||
g_assert (!dup_free);
|
||||
else
|
||||
g_assert (dup == dup_free);
|
||||
}
|
||||
|
||||
{
|
||||
gs_free char *dup_free = NULL;
|
||||
const char *dup;
|
||||
|
||||
l = nmtst_get_rand_int () % 23;
|
||||
dup = nm_strndup_a (10, input, l, &dup_free);
|
||||
g_assert (strncmp (dup, input, l) == 0);
|
||||
g_assert (strlen (dup) <= l);
|
||||
if (l < 10)
|
||||
g_assert (!dup_free);
|
||||
else
|
||||
g_assert (dup == dup_free);
|
||||
if (strlen (input) < l)
|
||||
g_assert (nm_utils_memeqzero (&dup[strlen (input)], l - strlen (input)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*****************************************************************************/
|
||||
|
||||
NMTST_DEFINE ();
|
||||
|
||||
int main (int argc, char **argv)
|
||||
|
|
@ -162,6 +214,7 @@ int main (int argc, char **argv)
|
|||
g_test_add_func ("/general/test_nmhash", test_nmhash);
|
||||
g_test_add_func ("/general/test_nm_make_strv", test_make_strv);
|
||||
g_test_add_func ("/general/test_nm_strdup_int", test_nm_strdup_int);
|
||||
g_test_add_func ("/general/test_nm_strndup_a", test_nm_strndup_a);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue