]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include/strutils: make xstrncpy() compatible with over-smart gcc 9
authorKarel Zak <kzak@redhat.com>
Tue, 17 Mar 2020 16:24:28 +0000 (17:24 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 13 Nov 2020 08:19:02 +0000 (09:19 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/strutils.h

index 4b3182f85c66aa8d8ea11357ba0e69930183bf77..4c5d18b4892514e22af5763350813ebf7604a4e8 100644 (file)
@@ -9,6 +9,8 @@
 #include <stdio.h>
 #include <errno.h>
 
+#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