From: Karel Zak Date: Tue, 17 Mar 2020 16:24:28 +0000 (+0100) Subject: include/strutils: make xstrncpy() compatible with over-smart gcc 9 X-Git-Tag: v2.37-rc1~378 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ad2659383c1b3e7b0f1174515730bafebbeadf84;p=thirdparty%2Futil-linux.git include/strutils: make xstrncpy() compatible with over-smart gcc 9 Signed-off-by: Karel Zak --- diff --git a/include/strutils.h b/include/strutils.h index 4b3182f85c..4c5d18b489 100644 --- a/include/strutils.h +++ b/include/strutils.h @@ -9,6 +9,8 @@ #include #include +#include "c.h" + /* initialize a custom exit code for all *_or_err functions */ extern void strutils_set_exitcode(int exit_code); @@ -61,8 +63,13 @@ extern char *strnchr(const char *s, size_t maxlen, int c); /* caller guarantees n > 0 */ static inline void xstrncpy(char *dest, const char *src, size_t n) { - strncpy(dest, src, n-1); - dest[n-1] = 0; + size_t len = src ? strlen(src) : 0; + + if (!len) + return; + len = min(len, n - 1); + memcpy(dest, src, len); + dest[len] = 0; } /* This is like strncpy(), but based on memcpy(), so compilers and static