summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-01-06 21:52:20 +0100
committerThomas Haller <thaller@redhat.com>2019-01-09 16:46:41 +0100
commit6ae04654f79643d758145df34b1011b051b1b329 (patch)
tree989fa25fac46ab034bcf97a1b87c53d788a680ed
parent1cd167c774c6e3730deba5a1932d83135a90b1ca (diff)
downloadNetworkManager-6ae04654f79643d758145df34b1011b051b1b329.tar.gz
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; ^~~~~~~~~~~~~~~~~
-rw-r--r--shared/nm-utils/nm-shared-utils.h14
1 files changed, 10 insertions, 4 deletions
diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h
index e560697b1a..f3f3831e49 100644
--- a/shared/nm-utils/nm-shared-utils.h
+++ b/shared/nm-utils/nm-shared-utils.h
@@ -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); \
})
/*****************************************************************************/