From: "Marek Behún" <kabel@kernel.org>
To: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Cc: "Simon Glass" <sjg@chromium.org>, "Tom Rini" <trini@konsulko.com>,
"U-Boot Mailing List" <u-boot@lists.denx.de>,
"Marek Behún" <marek.behun@nic.cz>
Subject: Re: [PATCH 08/10] env: Use strncpy() instead of ad-hoc code to copy variable value
Date: Tue, 12 Oct 2021 14:00:18 +0200 [thread overview]
Message-ID: <20211012140018.3a4502e6@dellmb> (raw)
In-Reply-To: <e0be2aac-afc6-a006-3e87-6d651dd1ea82@prevas.dk>
On Tue, 12 Oct 2021 13:41:43 +0200
Rasmus Villemoes <rasmus.villemoes@prevas.dk> wrote:
> On 12/10/2021 13.04, Marek Behún wrote:
> > From: Marek Behún <marek.behun@nic.cz>
> >
> > Copy the value of the found variable into given buffer with
> > strncpy().
> >
> > Signed-off-by: Marek Behún <marek.behun@nic.cz>
> > ---
> > cmd/nvedit.c | 15 +++++----------
> > 1 file changed, 5 insertions(+), 10 deletions(-)
> >
> > diff --git a/cmd/nvedit.c b/cmd/nvedit.c
> > index 412918a000..51b9e4ffa4 100644
> > --- a/cmd/nvedit.c
> > +++ b/cmd/nvedit.c
> > @@ -746,18 +746,13 @@ int env_get_f(const char *name, char *buf,
> > unsigned len) continue;
> >
> > /* found; copy out */
> > - for (n = 0; n < len; ++n, ++buf) {
> > - *buf = env[val++];
> > - if (*buf == '\0')
> > - return n;
> > + n = strncpy(buf, &env[val], len) - buf;
>
> strncpy by definition returns the dst argument, so this is, apart from
> the side effects done by strncpy, a complicated way of saying "n =
> 0;".
You are right, this is a mistake.
> > + if (len && n == len) {
>
> which then makes this automatically false always.
>
> I understand why you want to avoid an open-coded copying, but strncpy
> is almost certainly the wrong tool for the job. Apart from not
> revealing anything about the actual length of the source string, it
> also has the well-known defect of zeroing the tail of the buffer in
> the (usual) case where the source is shorter than the buffer.
U-Boot's strncpy does not do that:
* Note that unlike userspace strncpy, this does not %NUL-pad the
buffer.
* However, the result is not %NUL-terminated if the source exceeds
* @count bytes.
But you are right that I should use strlcpy here.
Thanks.
> n = strlcpy(buf, &env[val], len);
> if (n >= len) ...
>
> is probably closer to what you want. But only if you can trust
> &env[val] to actually have a nul byte before going into lala land.
>
> Rasmus
next prev parent reply other threads:[~2021-10-12 12:00 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-12 11:04 [PATCH 00/10] env_get_char() removal and env_get_f() refactor Marek Behún
2021-10-12 11:04 ` [PATCH 01/10] env: Drop env_get_char_spec() and old, unused .get_char() implementations Marek Behún
2021-10-12 11:04 ` [PATCH 02/10] examples: api: glue: Remove comment that does not apply anymore Marek Behún
2021-10-12 11:04 ` [PATCH 03/10] env: Change env_match() to static and remove from header Marek Behún
2021-10-12 11:04 ` [PATCH 04/10] env: Don't match empty variable name in env_match() Marek Behún
2021-10-12 11:04 ` [PATCH 05/10] env: Check for terminating null-byte " Marek Behún
2021-10-13 15:35 ` Marek Behún
2021-10-12 11:04 ` [PATCH 06/10] env: Inline env_get_char() into it's only user Marek Behún
2021-10-12 11:04 ` [PATCH 07/10] env: Early return from env_get_f() on NULL name Marek Behún
2021-10-12 11:04 ` [PATCH 08/10] env: Use strncpy() instead of ad-hoc code to copy variable value Marek Behún
2021-10-12 11:41 ` Rasmus Villemoes
2021-10-12 12:00 ` Marek Behún [this message]
2021-10-12 12:45 ` Rasmus Villemoes
2021-10-12 11:05 ` [PATCH 09/10] env: Use string pointer instead of indexes in env_get_f() Marek Behún
2021-10-12 11:05 ` [PATCH 10/10] env: Move non-cmd specific env functions to env/common.c Marek Behún
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=20211012140018.3a4502e6@dellmb \
--to=kabel@kernel.org \
--cc=marek.behun@nic.cz \
--cc=rasmus.villemoes@prevas.dk \
--cc=sjg@chromium.org \
--cc=trini@konsulko.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