From: Rodrigo Campos Date: Sun, 18 Feb 2024 19:51:05 +0000 (-0300) Subject: tools/nolibc: Fix strlcpy() return code and size usage X-Git-Tag: v6.10-rc1~179^2~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fbffce819e5ac151e137f881b89a9c1da0ebb76c;p=thirdparty%2Fkernel%2Flinux.git tools/nolibc: Fix strlcpy() return code and size usage The return code should always be strlen(src), and we should copy at most size-1 bytes. While we are there, make sure to null-terminate the dst buffer if we copied something. Signed-off-by: Rodrigo Campos Signed-off-by: Thomas Weißschuh --- diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index cc51fd6b63d02..565230a4ad472 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -219,16 +219,18 @@ static __attribute__((unused)) size_t strlcpy(char *dst, const char *src, size_t size) { size_t len; - char c; - for (len = 0;;) { - c = src[len]; - if (len < size) - dst[len] = c; - if (!c) - break; - len++; + for (len = 0; len < size; len++) { + dst[len] = src[len]; + if (!dst[len]) + return len; } + if (size) + dst[size-1] = '\0'; + + while (src[len]) + len++; + return len; }