From: Florian Forster Date: Thu, 11 Jan 2024 20:44:39 +0000 (+0100) Subject: common: Reserve a null byte when calling `strncpy`. X-Git-Tag: 6.0.0-rc0~13^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7de6192d5f827f0204cbf8c45ca921c3ed298cc8;p=thirdparty%2Fcollectd.git common: Reserve a null byte when calling `strncpy`. While `sstrncpy` guarantees a null terminated string, some compilers don't get the memo and complain about the buffer size being equal to the size provided to *strncpy(3)*. This *is* a potential source of error with *strncpy(3)*, because if the source string is longer than the buffer, the buffer is not null terminated. That is the precise reason `sstrncpy` exists in the first place. Make these compilers happy by decreasing the size passed to *strncpy(3)* by one. --- diff --git a/src/utils/common/common.c b/src/utils/common/common.c index ad72e93ff..31e6146af 100644 --- a/src/utils/common/common.c +++ b/src/utils/common/common.c @@ -85,8 +85,10 @@ static pthread_mutex_t strerror_r_lock = PTHREAD_MUTEX_INITIALIZER; #endif char *sstrncpy(char *dest, const char *src, size_t n) { - strncpy(dest, src, n); - dest[n - 1] = '\0'; + if (n > 0) { + strncpy(dest, src, n - 1); + dest[n - 1] = 0; + } return dest; } /* char *sstrncpy */