aboutsummaryrefslogtreecommitdiff
path: root/tools/include
diff options
context:
space:
mode:
authorRodrigo Campos2024-02-18 16:51:05 -0300
committerThomas Weißschuh2024-04-10 23:19:02 +0200
commitfbffce819e5ac151e137f881b89a9c1da0ebb76c (patch)
tree70547087330d00a1d215f4d01f018ac0988f5099 /tools/include
parent34d232c39a1e05ba734dc6ad9dc01d15788cd91d (diff)
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@sdfg.com.ar> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
Diffstat (limited to 'tools/include')
-rw-r--r--tools/include/nolibc/string.h18
1 files 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;
}