diff options
author | Sean Anderson | 2021-03-11 00:15:42 -0500 |
---|---|---|
committer | Tom Rini | 2021-04-12 17:44:55 -0400 |
commit | 9af869c4145a668b6db9accdea554eb57895a25e (patch) | |
tree | e37d12c248da4ec31b1569e1973598b924bb818b /lib | |
parent | d3358ecc54be0bc3b4dd11f7a63eab0a2842f772 (diff) |
lib: string: Implement strlcat
This introduces strlcat, which provides a safer interface than strncat. It
never copies more than its size bytes, including the terminating nul. In
addition, it never reads past dest[size - 1], even if dest is not
nul-terminated.
This also removes the stub for dwc3 now that we have a proper
implementation.
Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/string.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/lib/string.c b/lib/string.c index 1b867ac09d0..a0cff8fe88e 100644 --- a/lib/string.c +++ b/lib/string.c @@ -180,6 +180,25 @@ char * strncat(char *dest, const char *src, size_t count) } #endif +#ifndef __HAVE_ARCH_STRLCAT +/** + * strlcat - Append a length-limited, %NUL-terminated string to another + * @dest: The string to be appended to + * @src: The string to append to it + * @size: The size of @dest + * + * Compatible with *BSD: the result is always a valid NUL-terminated string that + * fits in the buffer (unless, of course, the buffer size is zero). It does not + * write past @size like strncat() does. + */ +size_t strlcat(char *dest, const char *src, size_t size) +{ + size_t len = strnlen(dest, size); + + return len + strlcpy(dest + len, src, size - len); +} +#endif + #ifndef __HAVE_ARCH_STRCMP /** * strcmp - Compare two strings |