mirror of
https://source.denx.de/u-boot/u-boot.git
synced 2026-06-02 09:46:37 +03:00
Revert "lib: string: Fix strlcpy return value", fix callers
Both the Linux kernel and libbsd agree that strlcpy() should always return strlen(src) and not include the NUL termination. The incorrect U-Boot implementation makes it impossible to check the return value for truncation, and breaks code written with the usual implementation in mind (for example, fdtdec_add_reserved_memory() was subtly broken). I reviewed all callers of strlcpy() and strlcat() and fixed them according to my understanding of the intended function. This reverts commitd3358ecc54and adds related fixes. Fixes:d3358ecc54("lib: string: Fix strlcpy return value") Signed-off-by: Matthias Schiffer <matthias.schiffer@ew.tq-group.com> Reviewed-by: Simon Glass <sjg@chromium.org> Reviewed-by: Sean Anderson <sean.anderson@seco.com>
This commit is contained in:
committed by
Tom Rini
parent
a169438411
commit
615828721a
14
lib/string.c
14
lib/string.c
@@ -116,20 +116,18 @@ char * strncpy(char * dest,const char *src,size_t count)
|
||||
* of course, the buffer size is zero). It does not pad
|
||||
* out the result like strncpy() does.
|
||||
*
|
||||
* Return: the number of bytes copied
|
||||
* Return: strlen(src)
|
||||
*/
|
||||
size_t strlcpy(char *dest, const char *src, size_t size)
|
||||
{
|
||||
if (size) {
|
||||
size_t srclen = strlen(src);
|
||||
size_t len = (srclen >= size) ? size - 1 : srclen;
|
||||
size_t ret = strlen(src);
|
||||
|
||||
if (size) {
|
||||
size_t len = (ret >= size) ? size - 1 : ret;
|
||||
memcpy(dest, src, len);
|
||||
dest[len] = '\0';
|
||||
return len + 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -191,6 +189,8 @@ char * strncat(char *dest, const char *src, size_t count)
|
||||
* 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.
|
||||
*
|
||||
* Return: min(strlen(dest), size) + strlen(src)
|
||||
*/
|
||||
size_t strlcat(char *dest, const char *src, size_t size)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user