shared: avoid compiler warning in nm_strndup_a()

Using strncpy() in the macro directly can result in a compiler warning.
We don't want to replace this with memcpy(), because strncpy() aborts
on the first NUL and fills the rest with NUL. Since nm_strndup_a() is a
replacement for g_strndup(), we want to do that here as well.

    In file included from ../shared/nm-default.h:294,
                     from ../libnm-core/nm-utils.c:22:
    ../libnm-core/nm-utils.c: In function nm_sock_addr_endpoint_new:
    ../shared/nm-utils/nm-shared-utils.h:281:4: error: strncpy output truncated before terminating nul copying as many bytes from a string as its length [-Werror=stringop-truncation]
        strncpy (_s, _str, _len); \
        ^~~~~~~~~~~~~~~~~~~~~~~~
    ../libnm-core/nm-utils.c:154:26: note: in expansion of macro nm_strndup_a
      host = _parse_endpoint (nm_strndup_a (200, endpoint, l_endpoint - 1, &host_clone),
                              ^~~~~~~~~~~~
    ../libnm-core/nm-utils.c:152:15: note: length computed here
      l_endpoint = strlen (endpoint) + 1;
                   ^~~~~~~~~~~~~~~~~
This commit is contained in:
Thomas Haller 2019-01-06 21:52:20 +01:00
parent 1cd167c774
commit 6ae04654f7

View file

@ -264,6 +264,15 @@ nm_memdup (gconstpointer data, gsize size)
return p;
}
static inline char *
_nm_strndup_a_step (char *s, const char *str, gsize len)
{
if (len > 0)
strncpy (s, str, len);
s[len] = '\0';
return s;
}
/* Similar to g_strndup(), however, if the string (including the terminating
* NUL char) fits into alloca_maxlen, this will alloca() the memory.
*
@ -289,10 +298,7 @@ nm_memdup (gconstpointer data, gsize size)
g_assert (_len < _alloca_maxlen); \
_s = g_alloca (_len + 1); \
} \
if (_len > 0) \
strncpy (_s, _str, _len); \
_s[_len] = '\0'; \
_s; \
_nm_strndup_a_step (_s, _str, _len); \
})
/*****************************************************************************/