Skip to contentSkip to conversation

image: Fix string truncation warning

PATCH In review Michal Suchanek 3 participants 5 messages Oct 13, 2022 – Oct 21, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 image: Fix 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);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---

Changes in v2:
Use __nonstring instead of suppressing the warning

---
 include/image.h  | 3 ++-
 tools/kwbimage.h | 6 ------
 2 files changed, 2 insertions(+), 7 deletions(-)
include/image.h +2 -1
index d7d6a3fe5b..82b447aeb7 100644
@@ -18,6 +18,7 @@
 #include "compiler.h"
 #include <asm/byteorder.h>
 #include <stdbool.h>
+#include <linux/compiler_attributes.h>
 
 /* Define this to avoid #ifdefs later on */
 struct lmb;
@@ -275,7 +276,7 @@ struct legacy_img_hdr {
 	uint8_t		ih_arch;	/* CPU architecture		*/
 	uint8_t		ih_type;	/* Image Type			*/
 	uint8_t		ih_comp;	/* Compression Type		*/
-	uint8_t		ih_name[IH_NMLEN];	/* Image Name		*/
+	uint8_t		ih_name[IH_NMLEN] __nonstring;	/* Image Name	*/
 };
 
 struct image_info {
tools/kwbimage.h +0 -6
index 505522332b..327ca34494 100644
@@ -11,12 +11,6 @@
 #include <compiler.h>
 #include <stdint.h>
 
-#ifdef __GNUC__
-#define __packed __attribute((packed))
-#else
-#define __packed
-#endif
-
 #define KWBIMAGE_MAX_CONFIG	((0x1dc - 0x20)/sizeof(struct reg_config))
 #define MAX_TEMPBUF_LEN		32
 
Signature · 1 line
2.37.3
#1 lore raw

PATCH v2 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 assumed 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.

---

(no changes since v1)

---
 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
#2 lore raw
In reply to Michal Suchanek
Quoted 7 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);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 52 lines

---

Changes in v2:
Use __nonstring instead of suppressing the warning

---
 include/image.h  | 3 ++-
 tools/kwbimage.h | 6 ------
 2 files changed, 2 insertions(+), 7 deletions(-)
include/image.h +2 -1
index d7d6a3fe5b..82b447aeb7 100644
@@ -18,6 +18,7 @@
 #include "compiler.h"
 #include <asm/byteorder.h>
 #include <stdbool.h>
+#include <linux/compiler_attributes.h>
 
 /* Define this to avoid #ifdefs later on */
 struct lmb;
@@ -275,7 +276,7 @@ struct legacy_img_hdr {
 	uint8_t		ih_arch;	/* CPU architecture		*/
 	uint8_t		ih_type;	/* Image Type			*/
 	uint8_t		ih_comp;	/* Compression Type		*/
-	uint8_t		ih_name[IH_NMLEN];	/* Image Name		*/
+	uint8_t		ih_name[IH_NMLEN] __nonstring;	/* Image Name	*/
 };
 
 struct image_info {
tools/kwbimage.h +0 -6
index 505522332b..327ca34494 100644
@@ -11,12 +11,6 @@
 #include <compiler.h>
 #include <stdint.h>
 
-#ifdef __GNUC__
-#define __packed __attribute((packed))
-#else
-#define __packed
-#endif
-
 #define KWBIMAGE_MAX_CONFIG	((0x1dc - 0x20)/sizeof(struct reg_config))
 #define MAX_TEMPBUF_LEN		32
 
Signature · 1 line
2.37.3
Reviewed-by
#3 lore raw

Hi,

Quoted 10 lines · Michal Suchánek
Quoted 7 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);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 11 lines
Quoted 10 lines

---

Changes in v2:
Use __nonstring instead of suppressing the warning

---
 include/image.h  | 3 ++-
 tools/kwbimage.h | 6 ------
 2 files changed, 2 insertions(+), 7 deletions(-)

+Tom Rini you can drop my one then:

https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/

#4 lore raw
In reply to Michal Suchanek
Quoted 8 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);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 6 lines

---

Changes in v2:
Use __nonstring instead of suppressing the warning

This breaks a ton of boards such as crs305-1g-4s in tools/kwbimage.h.

Signature · 1 line
Tom