[PATCH v2 29/45] image: Correct strncpy() warning with image_set_name()

Simon Glass sjg at chromium.org
Thu Oct 13 14:29:11 CEST 2022


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);
-- 
2.38.0.rc1.362.ged0d419d3c-goog



More information about the U-Boot mailing list