public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [PATCH] image: Ensure image header name is null terminated
@ 2022-08-23  5:59 Joel Stanley
  2022-08-23  7:27 ` Wolfgang Denk
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Joel Stanley @ 2022-08-23  5:59 UTC (permalink / raw)
  To: Simon Glass; +Cc: u-boot

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';
 }
 
 int image_check_hcrc(const image_header_t *hdr);
-- 
2.35.1


^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-09-14 22:39 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-08-23  5:59 [PATCH] image: Ensure image header name is null terminated Joel Stanley
2022-08-23  7:27 ` Wolfgang Denk
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox