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).
This commit is contained in:
Peter Hutterer 2023-05-18 11:57:09 +10:00
parent 3e2e43e352
commit 865d7152e0

View file

@ -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);