Skip to content

Commit fbffce8

Browse files
ratat-8ch
authored andcommitted
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>
1 parent 34d232c commit fbffce8

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

tools/include/nolibc/string.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,18 @@ static __attribute__((unused))
219219
size_t strlcpy(char *dst, const char *src, size_t size)
220220
{
221221
size_t len;
222-
char c;
223222

224-
for (len = 0;;) {
225-
c = src[len];
226-
if (len < size)
227-
dst[len] = c;
228-
if (!c)
229-
break;
230-
len++;
223+
for (len = 0; len < size; len++) {
224+
dst[len] = src[len];
225+
if (!dst[len])
226+
return len;
231227
}
228+
if (size)
229+
dst[size-1] = '\0';
230+
231+
while (src[len])
232+
len++;
233+
232234
return len;
233235
}
234236

0 commit comments

Comments
 (0)