[PATCH v2 29/45] image: Correct strncpy() warning with image_set_name()
Simon Glass
sjg at chromium.org
Fri Oct 14 17:55:58 CEST 2022
Hi Heinrich,
On Thu, 13 Oct 2022 at 10:23, Heinrich Schuchardt <xypron.glpk at gmx.de> wrote:
>
> On 10/13/22 14:29, Simon Glass wrote:
> > gcc 12 seems to warn on strncpy() as a matter of course. Rewrite the code
> > a different way to do the same thing, to avoid the warning.
> >
> > Signed-off-by: Simon Glass <sjg at chromium.org>
> > ---
> >
> > (no changes since v1)
> >
> > include/image.h | 8 +++++++-
> > 1 file changed, 7 insertions(+), 1 deletion(-)
> >
> > diff --git a/include/image.h b/include/image.h
> > index 7c91e705bf1..a8fd3e34287 100644
> > --- a/include/image.h
> > +++ b/include/image.h
> > @@ -853,7 +853,13 @@ image_set_hdr_b(comp) /* image_set_comp */
> >
> > static inline void image_set_name(struct legacy_img_hdr *hdr, const char *name)
> > {
> > - strncpy(image_get_name(hdr), name, IH_NMLEN);
> > + /*
> > + * This is equivalent to: strncpy(image_get_name(hdr), name, IH_NMLEN);
> > + *
> > + * Use the tortured code below to avoid a warning with gcc 12. We do not
> > + * want to include a nul terminator if the name is of length IH_NMLEN
> > + */
> > + memcpy(image_get_name(hdr), name, strnlen(name, IH_NMLEN));
> > }
> >
> > int image_check_hcrc(const struct legacy_img_hdr *hdr);
>
> memcpy does not add a trailing '\0' if there is enough space.
>
> In a parallel thread today it was indicated that the proper way of
> addressing the problem is using __attribute__(nonstring), cf.
>
> https://lore.kernel.org/u-boot/dad17a9f-d823-1e8b-3381-53961294521c@prevas.dk/
Yes, I pointed Tom to it instead of this. That was the question I
asked a month ago but I didn't not know how to find this option.
Regards,
Simon
More information about the U-Boot
mailing list