public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Sean Anderson <seanga2@gmail.com>
To: u-boot@lists.denx.de
Subject: [PATCH v2 1/5] lib: string: Fix strlcpy return value
Date: Thu, 11 Mar 2021 00:15:41 -0500	[thread overview]
Message-ID: <20210311051545.1886333-2-seanga2@gmail.com> (raw)
In-Reply-To: <20210311051545.1886333-1-seanga2@gmail.com>

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>
---

Changes in v2:
- New

 lib/string.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/lib/string.c b/lib/string.c
index 73b984123d..1b867ac09d 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
 
-- 
2.30.1

  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 ` Sean Anderson [this message]
2021-03-25  0:38   ` [PATCH v2 1/5] lib: string: Fix strlcpy return value 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 ` [PATCH v2 2/5] lib: string: Implement strlcat Sean Anderson
2021-03-25  0:38   ` 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-2-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