Skip to contentSkip to conversation

usb: typec: avoid format-overflow warning

PATCH In review Arnd Bergmann 2 participants 2 messages Jul 6, 2018 – Jul 9, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: typec: avoid format-overflow warning

gcc-8 points out that the fix-byte buffer might be too small if desc->mode is a three-digit number:

drivers/usb/typec/class.c: In function 'typec_register_altmode':
drivers/usb/typec/class.c:502:32: error: '%d' directive writing between 1 and 3 bytes into a region of size 2 [-Werror=format-overflow=]

  sprintf(alt->group_name, "mode%d", desc->mode);
                                ^~
drivers/usb/typec/class.c:502:27: note: directive argument in the range [0, 255]
  sprintf(alt->group_name, "mode%d", desc->mode);
                           ^~~~~~~~

drivers/usb/typec/class.c:502:2: note: 'sprintf' output between 6 and 8 bytes into a destination of size 6
sprintf(alt->group_name, "mode%d", desc->mode); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I assume this cannot happen in practice, but we can simply make the string long enough to avoid the warning. This uses the two padding bytes that already exist after the string.

Fixes 4ab8c18d4d67 ("usb: typec: Register a device for every mode")
---
 drivers/usb/typec/bus.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/typec/bus.h +1 -1
index 62aaf8b56bde..db40e61d8b72 100644
@@ -16,7 +16,7 @@ struct altmode {
 	enum typec_port_data		roles;
 
 	struct attribute		*attrs[5];
-	char				group_name[6];
+	char				group_name[8];
 	struct attribute_group		group;
 	const struct attribute_group	*groups[2];
 
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 14 lines · Arnd Bergmann

gcc-8 points out that the fix-byte buffer might be too small if desc->mode is a three-digit number:

drivers/usb/typec/class.c: In function 'typec_register_altmode':
drivers/usb/typec/class.c:502:32: error: '%d' directive writing between 1 and 3 bytes into a region of size 2 [-Werror=format-overflow=]

  sprintf(alt->group_name, "mode%d", desc->mode);
                                ^~
drivers/usb/typec/class.c:502:27: note: directive argument in the range [0, 255]
  sprintf(alt->group_name, "mode%d", desc->mode);
                           ^~~~~~~~

drivers/usb/typec/class.c:502:2: note: 'sprintf' output between 6 and 8 bytes into a destination of size 6
sprintf(alt->group_name, "mode%d", desc->mode); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Quoted 6 lines

I assume this cannot happen in practice, but we can simply make the string long enough to avoid the warning. This uses the two padding bytes that already exist after the string.

Fixes 4ab8c18d4d67 ("usb: typec: Register a device for every mode")
Quoted 9 lines
---
 drivers/usb/typec/bus.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/typec/bus.h +0 -0
index 62aaf8b56bde..db40e61d8b72 100644

drivers/usb/typec/bus.h struct altmode { · line 22

Quoted 8 lines
drivers/usb/typec/bus.h +1 -1
@@ -16,7 +16,7 @@ struct altmode {
 	enum typec_port_data		roles;
 
 	struct attribute		*attrs[5];
-	char				group_name[6];
+	char				group_name[8];
 	struct attribute_group		group;
 	const struct attribute_group	*groups[2];
 

Thanks,