]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
tools/nolibc: Fix strlcpy() return code and size usage
authorRodrigo Campos <rodrigo@sdfg.com.ar>
Sun, 18 Feb 2024 19:51:05 +0000 (16:51 -0300)
committerThomas Weißschuh <linux@weissschuh.net>
Wed, 10 Apr 2024 21:19:02 +0000 (23:19 +0200)
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 <rodrigo@sdfg.com.ar>
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
tools/include/nolibc/string.h

index cc51fd6b63d02b19b324c9a589cba197f73b6a3b..565230a4ad4724dab41dd61bdb65850f9c6ca3ee 100644 (file)
@@ -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;
 }