All of lore.kernel.org
 help / color / mirror / Atom feed
From: Wolfgang Denk <wd@denx.de>
To: Joel Stanley <joel@jms.id.au>
Cc: Simon Glass <sjg@chromium.org>, u-boot@lists.denx.de
Subject: Re: [PATCH] image: Ensure image header name is null terminated
Date: Tue, 23 Aug 2022 09:27:50 +0200	[thread overview]
Message-ID: <1878698.1661239670@gemini.denx.de> (raw)
In-Reply-To: <20220823055907.416060-1-joel@jms.id.au>

Dear Joel,

In message <20220823055907.416060-1-joel@jms.id.au> you wrote:
> When building with GCC 12:
>
> ../include/image.h:779:9: warning: ‘strncpy’ specified bound 32 equals destination size [-Wstringop-truncation]
>   779 |         strncpy(image_get_name(hdr), name, IH_NMLEN);
>       |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Ensure the copied string is null terminated by always setting the final
> byte to 0. Shorten the strncpy to IH_NMLEN-1 as we will always overwrite
> the last byte.
>
> We can't use strlcpy as this is code is built on the host as well as the
> target.
>
> Fixes: b97a2a0a21f2 ("[new uImage] Define a API for image handling operations")
> Signed-off-by: Joel Stanley <joel@jms.id.au>
> ---
>  include/image.h | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/include/image.h b/include/image.h
> index e4c6a50b885f..665b2278b7fb 100644
> --- a/include/image.h
> +++ b/include/image.h
> @@ -776,7 +776,10 @@ image_set_hdr_b(comp)		/* image_set_comp */
>  
>  static inline void image_set_name(image_header_t *hdr, const char *name)
>  {
> -	strncpy(image_get_name(hdr), name, IH_NMLEN);
> +	char *hdr_name = image_get_name(hdr);
> +
> +	strncpy(hdr_name, name, IH_NMLEN - 1);
> +	hdr_name[IH_NMLEN - 1] = '\0';
>  }

Why don't you use strlcpy() instead?  This covers exactly the
situation we have here.

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich,  Office: Kirchenstr. 5, 82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd@denx.de
panic: can't find /

  reply	other threads:[~2022-08-23  7:28 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-23  5:59 [PATCH] image: Ensure image header name is null terminated Joel Stanley
2022-08-23  7:27 ` Wolfgang Denk [this message]
2022-08-23  9:46 ` John Keeping
2022-08-23 13:38   ` Simon Glass
2022-08-23 14:11     ` Rasmus Villemoes
2022-09-14 22:11 ` Tom Rini
2022-09-14 22:39   ` Simon Glass

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=1878698.1661239670@gemini.denx.de \
    --to=wd@denx.de \
    --cc=joel@jms.id.au \
    --cc=sjg@chromium.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.