From: Urban Wallasch Date: Thu, 23 Apr 2020 09:19:25 +0000 (+0200) Subject: * Re-implemented str_ncpy() without the use of strncat() to avoid -Werror=stringop... X-Git-Url: https://git.packet-gain.de/?a=commitdiff_plain;h=HEAD;p=oddbits.git * Re-implemented str_ncpy() without the use of strncat() to avoid -Werror=stringop-truncation. --- diff --git a/str/str.h b/str/str.h index 0005b8b..4736ce4 100644 --- a/str/str.h +++ b/str/str.h @@ -475,8 +475,12 @@ static inline size_t str_nlen(const char *s, size_t maxlen) { * The buffer dest must be large enough to hold at least n+1 characters. */ static inline char *str_ncpy(char *dest, const char *src, size_t n) { - *dest = '\0'; - return strncat(dest, src, n); + char *d = dest; + + while ( *src && n-- ) + *d++ = *src++; + *d = '\0'; + return dest; } diff --git a/str/str_test.c b/str/str_test.c index 3520fd1..5a807bf 100644 --- a/str/str_test.c +++ b/str/str_test.c @@ -400,7 +400,11 @@ int main(void) { { H("str_ncpy"); - char s[11] = { 0 }; + char s[11] = "_deadbeef_"; + D(s); + T(strcmp(str_ncpy(s, "foobar", 0), "") == 0); + D(s); + T(strcmp(str_ncpy(s, "foobar", 1), "f") == 0); D(s); T(strcmp(str_ncpy(s, "foobar", 10), "foobar") == 0); D(s);