mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git
synced 2025-01-17 22:05:08 +00:00
tools/nolibc: Fix strlcat() return code and size usage
The return code should always be strlen(src) + strnlen(dst, size). Let's make sure to copy at most size-1 bytes from src and null-terminate the dst buffer if we did copied something. While we can use strnlen() and strncpy() to implement strlcat(), this is simple enough and results in shorter code when compiled. Signed-off-by: Rodrigo Campos <rodrigo@sdfg.com.ar> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
This commit is contained in:
parent
689230b674
commit
34d232c39a
@ -187,22 +187,31 @@ char *strndup(const char *str, size_t maxlen)
|
||||
static __attribute__((unused))
|
||||
size_t strlcat(char *dst, const char *src, size_t size)
|
||||
{
|
||||
size_t len;
|
||||
char c;
|
||||
size_t len = 0;
|
||||
|
||||
for (len = 0; dst[len]; len++)
|
||||
;
|
||||
for (; len < size; len++) {
|
||||
if (dst[len] == '\0')
|
||||
break;
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
c = *src;
|
||||
if (len < size)
|
||||
dst[len] = c;
|
||||
if (!c)
|
||||
/*
|
||||
* We want len < size-1. But as size is unsigned and can wrap
|
||||
* around, we use len + 1 instead.
|
||||
*/
|
||||
while (len + 1 < size) {
|
||||
dst[len] = *src;
|
||||
if (*src == '\0')
|
||||
break;
|
||||
len++;
|
||||
src++;
|
||||
}
|
||||
|
||||
if (len < size)
|
||||
dst[len] = '\0';
|
||||
|
||||
while (*src++)
|
||||
len++;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user