From: Sean Anderson <seanga2@gmail.com>
To: u-boot@lists.denx.de
Subject: [PATCH v2 2/5] lib: string: Implement strlcat
Date: Thu, 11 Mar 2021 00:15:42 -0500 [thread overview]
Message-ID: <20210311051545.1886333-3-seanga2@gmail.com> (raw)
In-Reply-To: <20210311051545.1886333-1-seanga2@gmail.com>
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>
---
Changes in v2:
- New
drivers/usb/dwc3/linux-compat.h | 6 ------
include/linux/string.h | 3 +++
lib/string.c | 19 +++++++++++++++++++
3 files changed, 22 insertions(+), 6 deletions(-)
diff --git a/drivers/usb/dwc3/linux-compat.h b/drivers/usb/dwc3/linux-compat.h
index 82793765be..3bb0bda5a6 100644
--- a/drivers/usb/dwc3/linux-compat.h
+++ b/drivers/usb/dwc3/linux-compat.h
@@ -13,10 +13,4 @@
#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
diff --git a/include/linux/string.h b/include/linux/string.h
index d67998e5c4..dd255f2163 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -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
diff --git a/lib/string.c b/lib/string.c
index 1b867ac09d..a0cff8fe88 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
--
2.30.1
next prev parent reply other threads:[~2021-03-11 5:15 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-11 5:15 [PATCH v2 0/5] string: strl(cat|cpy) Sean Anderson
2021-03-11 5:15 ` [PATCH v2 1/5] lib: string: Fix strlcpy return value Sean Anderson
2021-03-25 0:38 ` Simon Glass
2021-03-25 0:54 ` Sean Anderson
2021-03-25 2:40 ` Simon Glass
2021-03-25 0:53 ` Sean Anderson
2021-04-13 14:28 ` Tom Rini
2021-03-11 5:15 ` Sean Anderson [this message]
2021-03-25 0:38 ` [PATCH v2 2/5] lib: string: Implement strlcat Simon Glass
2021-04-13 14:28 ` Tom Rini
2021-03-11 5:15 ` [PATCH v2 3/5] test: Add test for strlcat Sean Anderson
2021-03-25 0:38 ` Simon Glass
2021-04-13 14:29 ` Tom Rini
2021-03-11 5:15 ` [PATCH v2 4/5] fastboot: Fix possible buffer overrun Sean Anderson
2021-04-13 14:29 ` Tom Rini
2021-03-11 5:15 ` [PATCH v2 5/5] checkpatch: Add warnings for using strn(cat|cpy) Sean Anderson
2021-03-25 0:38 ` Simon Glass
2021-04-13 14:29 ` Tom Rini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210311051545.1886333-3-seanga2@gmail.com \
--to=seanga2@gmail.com \
--cc=u-boot@lists.denx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox