From: Sean Anderson <seanga2@gmail.com>
To: u-boot@lists.denx.de
Subject: [PATCH v2 1/5] lib: string: Fix strlcpy return value
Date: Wed, 24 Mar 2021 20:53:43 -0400 [thread overview]
Message-ID: <b88f9964-5a71-dadc-3f59-a7aa7cc047cd@gmail.com> (raw)
In-Reply-To: <20210311051545.1886333-2-seanga2@gmail.com>
On 3/11/21 12:15 AM, Sean Anderson wrote:
> strlcpy should always return the number of bytes copied. We were
> accidentally missing the nul-terminator. We also always used to return a
It looks like I was a bit bullish in assuming a mistake. After reviewing
the man page, it looks like the nul-terminator is intentionally
excluded.
> The strlcpy() and strlcat() functions return the total length of the
> string they tried to create. For strlcpy() that means the length of
> src. For strlcat() that means the initial length of dst plus the
> length of src. While this may seem somewhat confusing, it was done to
> make truncation detection simple.
In particular, we should return the length of the string, which may be
different from the amount of bytes copied. However, the non-zero return
value should be fixed.
> 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
>
>
next prev parent reply other threads:[~2021-03-25 0:53 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 [this message]
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=b88f9964-5a71-dadc-3f59-a7aa7cc047cd@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