lib: string: Implement strlcat
authorSean Anderson <seanga2@gmail.com>
Thu, 11 Mar 2021 05:15:42 +0000 (00:15 -0500)
committerTom Rini <trini@konsulko.com>
Mon, 12 Apr 2021 21:44:55 +0000 (17:44 -0400)
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>
drivers/usb/dwc3/linux-compat.h
include/linux/string.h
lib/string.c

index 8279376..3bb0bda 100644 (file)
 
 #define dev_WARN(dev, format, arg...)  debug(format, ##arg)
 
-static inline size_t strlcat(char *dest, const char *src, size_t n)
-{
-       strcat(dest, src);
-       return strlen(dest) + strlen(src);
-}
-
 #endif
index d67998e..dd255f2 100644 (file)
@@ -35,6 +35,9 @@ extern char * strcat(char *, const char *);
 #ifndef __HAVE_ARCH_STRNCAT
 extern char * strncat(char *, const char *, __kernel_size_t);
 #endif
+#ifndef __HAVE_ARCH_STRLCAT
+size_t strlcat(char *, const char *, size_t);
+#endif
 #ifndef __HAVE_ARCH_STRCMP
 extern int strcmp(const char *,const char *);
 #endif
index 1b867ac..a0cff8f 100644 (file)
@@ -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