Skip to contentSkip to conversation

image: Suppress string truncation warning

PATCH Superseded Michal Suchanek 3 participants 5 messages Oct 12, 2022 – Oct 13, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 image: Suppress string truncation warning

In file included from ../tools/imagetool.h:24,

                 from ../tools/default_image.c:16:
In function ‘image_set_name’,
    inlined from ‘image_set_header’ at ../tools/default_image.c:133:2:
../include/image.h:786:9: warning: ‘strncpy’ specified bound 32 equals destination size [-Wstringop-truncation]
  786 |         strncpy(image_get_name(hdr), name, IH_NMLEN);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There is no standard function that can be used to make the copy without a warning.

---

 include/image.h | 7 +++++++
 1 file changed, 7 insertions(+)
include/image.h +7 -0
index d7d6a3fe5b..e08ef6ed55 100644
@@ -783,7 +783,14 @@ image_set_hdr_b(comp)		/* image_set_comp */
 
 static inline void image_set_name(struct legacy_img_hdr *hdr, const char *name)
 {
+#if defined(__GNUC__) && (__GNUC__ > 7)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wstringop-truncation"
+#endif
 	strncpy(image_get_name(hdr), name, IH_NMLEN);
+#if defined(__GNUC__) && (__GNUC__ > 7)
+#pragma GCC diagnostic pop
+#endif
 }
 
 int image_check_hcrc(const struct legacy_img_hdr *hdr);
Signature · 1 line
2.37.3
#1 lore raw

PATCH 2/2 spl: spl_legacy: Fix printing image name

image_set_name does not terminate the name in the image header.

Then is should not be assument it's nul terminated. image_print_contents correctly print only IH_NMLEN characters.

Fix printing the unterminated field in spl_parse_legacy_header. The format specifies the minimum printed length, not maximum.

---

 common/spl/spl_legacy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/spl/spl_legacy.c +1 -1
index b3624dfbb7..11c459254b 100644
@@ -55,7 +55,7 @@ int spl_parse_legacy_header(struct spl_image_info *spl_image,
 	spl_image->os = image_get_os(header);
 	spl_image->name = image_get_name(header);
 	debug(SPL_TPL_PROMPT
-	      "payload image: %32s load addr: 0x%lx size: %d\n",
+	      "payload image: %.*s load addr: 0x%lx size: %d\n", IH_NMLEN,
 	      spl_image->name, spl_image->load_addr, spl_image->size);
 
 	return 0;
Signature · 1 line
2.37.3
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] spl: spl_legacy: Fix printing image name

Quoted 6 lines · Michal Suchanek

image_set_name does not terminate the name in the image header.

Then is should not be assument it's nul terminated. image_print_contents correctly print only IH_NMLEN characters.

Quoted 8 lines

Fix printing the unterminated field in spl_parse_legacy_header. The format specifies the minimum printed length, not maximum.

---

 common/spl/spl_legacy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#3 lore raw
In reply to Michal Suchanek
Quoted 10 lines · Michal Suchanek

In file included from ../tools/imagetool.h:24,

                 from ../tools/default_image.c:16:
In function ‘image_set_name’,
    inlined from ‘image_set_header’ at ../tools/default_image.c:133:2:
../include/image.h:786:9: warning: ‘strncpy’ specified bound 32 equals destination size [-Wstringop-truncation]
  786 |         strncpy(image_get_name(hdr), name, IH_NMLEN);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There is no standard function that can be used to make the copy without a warning.

True, but the compiler does give a way to inform that the destination is not _supposed_ to be a nul-terminated string.

https://lore.kernel.org/u-boot/[email protected]/

And our include/linux/compiler_attributes.h indeed already exposes that __nonstring attribute. Perhaps try applying that to the ih_name member.

It may also be necessary to drop the image_get_name() indirection and just use hdr->ih_name directly; though gcc obviously sees through it to account for the size of the destination buffer (otherwise it couldn't warn), I'm not sure it won't lose that attribute.

Rasmus

#4 lore raw
Quoted 12 lines · Rasmus Villemoes
Quoted 10 lines · Michal Suchanek

In file included from ../tools/imagetool.h:24,

                 from ../tools/default_image.c:16:
In function ‘image_set_name’,
    inlined from ‘image_set_header’ at ../tools/default_image.c:133:2:
../include/image.h:786:9: warning: ‘strncpy’ specified bound 32 equals destination size [-Wstringop-truncation]
  786 |         strncpy(image_get_name(hdr), name, IH_NMLEN);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

There is no standard function that can be used to make the copy without a warning.

Quoted 7 lines

True, but the compiler does give a way to inform that the destination is not _supposed_ to be a nul-terminated string.

https://lore.kernel.org/u-boot/[email protected]/

And our include/linux/compiler_attributes.h indeed already exposes that __nonstring attribute. Perhaps try applying that to the ih_name member.

That's better, that's an actual fix.

Thanks

Michal