[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240218195110.1386840-4-rodrigo@sdfg.com.ar>
Date: Sun, 18 Feb 2024 16:51:05 -0300
From: Rodrigo Campos <rodrigo@...g.com.ar>
To: Willy Tarreau <w@....eu>,
Thomas Weißschuh <linux@...ssschuh.net>
Cc: linux-kernel@...r.kernel.org,
Rodrigo Campos <rodrigo@...g.com.ar>
Subject: [PATCH v3 3/4] 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 <rodrigo@...g.com.ar>
---
tools/include/nolibc/string.h | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h
index cc51fd6b63d0..565230a4ad47 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;
}
--
2.43.0
Powered by blists - more mailing lists