From 865d7152e04da740fd0911bf1714fe3482c7dc3a Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Thu, 18 May 2023 11:57:09 +1000 Subject: [PATCH] util: replace a strncpy with a memcpy We know exactly the lengths of everything involved here so let's use memcpy. This way we don't need the stringop-truncation warning (a pragma clang doesn't support anyway). --- src/util-strings.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/util-strings.c b/src/util-strings.c index e0a2c6b..d55f774 100644 --- a/src/util-strings.c +++ b/src/util-strings.c @@ -184,9 +184,6 @@ strreplace(const char *string, const char *separator, const char *replacement) char *r = calloc(max + 1, 1); /* the result, one extra for terminating \0 */ char *destptr = r; -/* our strncpy calls truncate the second argument, we know... */ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wstringop-truncation" while (next) { size_t len = next - current; /* silently truncate because we really don't care about this @@ -195,11 +192,11 @@ strreplace(const char *string, const char *separator, const char *replacement) break; /* Copy the source string over, then append the separator */ - strncpy(destptr, current, len); + memcpy(destptr, current, len); destptr += len; if (destptr + rlen > r + max) break; - strncpy(destptr, replacement, rlen); + memcpy(destptr, replacement, rlen); destptr += rlen; current = next + splen; @@ -213,10 +210,9 @@ strreplace(const char *string, const char *separator, const char *replacement) /* silently truncate because we really don't care about this * case */ if (destptr + len <= r + max) { - strncpy(destptr, current, len); + memcpy(destptr, current, len); destptr += len; } -#pragma GCC diagnostic pop void *tmp = realloc(r, (destptr - r) + 1); assert(tmp);