aboutsummaryrefslogtreecommitdiff
path: root/lib/string.c
diff options
context:
space:
mode:
authorSean Anderson2021-03-11 00:15:41 -0500
committerTom Rini2021-04-12 17:44:55 -0400
commitd3358ecc54be0bc3b4dd11f7a63eab0a2842f772 (patch)
treede594fc4a6e853b3a94e829ff92d60446ebd2a1c /lib/string.c
parent92e84896112037833e429d629f87cedbeb255d5a (diff)
lib: string: Fix strlcpy return value
strlcpy should always return the number of bytes copied. We were accidentally missing the nul-terminator. We also always used to return a non-zero value, even if we did not actually copy anything. Fixes: 23cd138503 ("Integrate USB gadget layer and USB CDC driver layer") Signed-off-by: Sean Anderson <seanga2@gmail.com>
Diffstat (limited to 'lib/string.c')
-rw-r--r--lib/string.c12
1 files changed, 8 insertions, 4 deletions
diff --git a/lib/string.c b/lib/string.c
index 73b984123dc..1b867ac09d0 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -114,17 +114,21 @@ char * strncpy(char * dest,const char *src,size_t count)
* NUL-terminated string that fits in the buffer (unless,
* of course, the buffer size is zero). It does not pad
* out the result like strncpy() does.
+ *
+ * Return: the number of bytes copied
*/
size_t strlcpy(char *dest, const char *src, size_t size)
{
- size_t ret = strlen(src);
-
if (size) {
- size_t len = (ret >= size) ? size - 1 : ret;
+ size_t srclen = strlen(src);
+ size_t len = (srclen >= size) ? size - 1 : srclen;
+
memcpy(dest, src, len);
dest[len] = '\0';
+ return len + 1;
}
- return ret;
+
+ return 0;
}
#endif