Skip to contentSkip to conversation

usb: hcd: Bump local buffer size in rh_string()

PATCH In review Andy Shevchenko 3 participants 7 messages Jan 16, 2025 – Jan 17, 2025 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: hcd: Bump local buffer size in rh_string()

GCC is not happy about the buffer size:

drivers/usb/core/hcd.c:441:48: error: ‘%s’ directive output may be truncated writing up to 64 bytes into a region of size between 35 and 99 [-Werror=format-truncation=]

  441 |                 snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
      |                                                ^~
  442 |                         init_utsname()->release, hcd->driver->description);
      |                         ~~~~~~~~~~~~~~~~~~~~~~~

Bump the size to get it enough for the possible strings.

---
 drivers/usb/core/hcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/core/hcd.c +1 -1
index 0b2490347b9f..a75cf1f6d741 100644
@@ -415,7 +415,7 @@ ascii2desc(char const *s, u8 *buf, unsigned len)
 static unsigned
 rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len)
 {
-	char buf[100];
+	char buf[160];
 	char const *s;
 	static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};
 
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
#1 lore raw
Quoted 20 lines · Andy Shevchenko

GCC is not happy about the buffer size:

drivers/usb/core/hcd.c:441:48: error: ‘%s’ directive output may be truncated writing up to 64 bytes into a region of size between 35 and 99 [-Werror=format-truncation=]

  441 |                 snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
      |                                                ^~
  442 |                         init_utsname()->release, hcd->driver->description);
      |                         ~~~~~~~~~~~~~~~~~~~~~~~

Bump the size to get it enough for the possible strings.

---
 drivers/usb/core/hcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/core/hcd.c +0 -0
index 0b2490347b9f..a75cf1f6d741 100644

drivers/usb/core/hcd.c ascii2desc(char const *s, u8 *buf, unsigned len) · line 420

Quoted 7 lines
drivers/usb/core/hcd.c +1 -1
@@ -415,7 +415,7 @@ ascii2desc(char const *s, u8 *buf, unsigned len)
 static unsigned
 rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len)
 {
-	char buf[100];
+	char buf[160];
 	char const *s;
 	static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};

Worst case it's properly truncated so why do we need to worry about this "warning"? And what compiler version is giving that, I don't see that here in my build testing.

thanks,

greg k-h

#2 lore raw
Quoted 10 lines · Greg Kroah-Hartman
Quoted 9 lines · Andy Shevchenko

GCC is not happy about the buffer size:

drivers/usb/core/hcd.c:441:48: error: ‘%s’ directive output may be truncated writing up to 64 bytes into a region of size between 35 and 99 [-Werror=format-truncation=]

  441 |                 snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
      |                                                ^~
  442 |                         init_utsname()->release, hcd->driver->description);
      |                         ~~~~~~~~~~~~~~~~~~~~~~~

Bump the size to get it enough for the possible strings.

...

Quoted 10 lines

drivers/usb/core/hcd.c ascii2desc(char const *s, u8 *buf, unsigned len) · line 420

Quoted 7 lines

static unsigned
rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len) {

drivers/usb/core/hcd.c +1 -1
-	char buf[100];
+	char buf[160];
 	char const *s;
 	static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};

Worst case it's properly truncated so why do we need to worry about this "warning"?

With CONFIG_WERROR=y it's a compilation error. My goal is to have i386_defconfig and x86_64_defconfig to be compiled with make W=1.

Quoted 2 lines

And what compiler version is giving that, I don't see that here in my build testing.

make W=1 (and be sure that CONFIG_WERROR=y).

$ gcc --version
gcc (Debian 14.2.0-12) 14.2.0

(IIRC that had been started with any GCC from v13?)

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 25 lines · Andy Shevchenko
Quoted 10 lines · Greg Kroah-Hartman
Quoted 9 lines · Andy Shevchenko

GCC is not happy about the buffer size:

drivers/usb/core/hcd.c:441:48: error: ‘%s’ directive output may be truncated writing up to 64 bytes into a region of size between 35 and 99 [-Werror=format-truncation=]

  441 |                 snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
      |                                                ^~
  442 |                         init_utsname()->release, hcd->driver->description);
      |                         ~~~~~~~~~~~~~~~~~~~~~~~

Bump the size to get it enough for the possible strings.

...

Quoted 10 lines

drivers/usb/core/hcd.c ascii2desc(char const *s, u8 *buf, unsigned len) · line 420

Quoted 7 lines

static unsigned
rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len) {

drivers/usb/core/hcd.c +1 -1
-	char buf[100];
+	char buf[160];
 	char const *s;
 	static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};

Worst case it's properly truncated so why do we need to worry about this "warning"?

Quoted 2 lines

With CONFIG_WERROR=y it's a compilation error. My goal is to have i386_defconfig and x86_64_defconfig to be compiled with make W=1.

So you have to have W=1 enabled, right? On my normal builds, with CONFIG_WERROR=y enabled, I do not see this.

Quoted 4 lines
Quoted 2 lines

And what compiler version is giving that, I don't see that here in my build testing.

make W=1 (and be sure that CONFIG_WERROR=y).

Ah, ok, manual work here.

And I guess the error is right, ->sysname could be 64 and release can also be 64 bytes long, so it would be truncated.

thanks,

greg k-h

#4 lore raw
Quoted 12 lines · Greg Kroah-Hartman
Quoted 11 lines · Andy Shevchenko
Quoted 10 lines · Greg Kroah-Hartman
Quoted 9 lines · Andy Shevchenko

GCC is not happy about the buffer size:

drivers/usb/core/hcd.c:441:48: error: ‘%s’ directive output may be truncated writing up to 64 bytes into a region of size between 35 and 99 [-Werror=format-truncation=]

  441 |                 snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
      |                                                ^~
  442 |                         init_utsname()->release, hcd->driver->description);
      |                         ~~~~~~~~~~~~~~~~~~~~~~~

Bump the size to get it enough for the possible strings.

...

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

drivers/usb/core/hcd.c ascii2desc(char const *s, u8 *buf, unsigned len) · line 420

Quoted 7 lines

static unsigned
rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len) {

drivers/usb/core/hcd.c +1 -1
-	char buf[100];
+	char buf[160];
 	char const *s;
 	static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};

Worst case it's properly truncated so why do we need to worry about this "warning"?

Quoted 4 lines
Quoted 2 lines

With CONFIG_WERROR=y it's a compilation error. My goal is to have i386_defconfig and x86_64_defconfig to be compiled with make W=1.

So you have to have W=1 enabled, right?

Yep!

Quoted 11 lines

On my normal builds, with CONFIG_WERROR=y enabled, I do not see this.

Quoted 4 lines
Quoted 2 lines

And what compiler version is giving that, I don't see that here in my build testing.

make W=1 (and be sure that CONFIG_WERROR=y).

Ah, ok, manual work here.

And I guess the error is right, ->sysname could be 64 and release can also be 64 bytes long, so it would be truncated.

Yeah... Should I update the commit message and issue v2?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 39 lines · Andy Shevchenko
Quoted 12 lines · Greg Kroah-Hartman
Quoted 11 lines · Andy Shevchenko
Quoted 10 lines · Greg Kroah-Hartman
Quoted 9 lines · Andy Shevchenko

GCC is not happy about the buffer size:

drivers/usb/core/hcd.c:441:48: error: ‘%s’ directive output may be truncated writing up to 64 bytes into a region of size between 35 and 99 [-Werror=format-truncation=]

  441 |                 snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
      |                                                ^~
  442 |                         init_utsname()->release, hcd->driver->description);
      |                         ~~~~~~~~~~~~~~~~~~~~~~~

Bump the size to get it enough for the possible strings.

...

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

drivers/usb/core/hcd.c ascii2desc(char const *s, u8 *buf, unsigned len) · line 420

Quoted 7 lines

static unsigned
rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len) {

drivers/usb/core/hcd.c +1 -1
-	char buf[100];
+	char buf[160];
 	char const *s;
 	static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};

Worst case it's properly truncated so why do we need to worry about this "warning"?

Quoted 4 lines
Quoted 2 lines

With CONFIG_WERROR=y it's a compilation error. My goal is to have i386_defconfig and x86_64_defconfig to be compiled with make W=1.

So you have to have W=1 enabled, right?

Yep!

Quoted 5 lines

On my normal builds, with CONFIG_WERROR=y enabled, I do not see this.

Quoted 3 lines
Quoted 2 lines

And what compiler version is giving that, I don't see that here in my build testing.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

make W=1 (and be sure that CONFIG_WERROR=y).

Ah, ok, manual work here.

And I guess the error is right, ->sysname could be 64 and release can also be 64 bytes long, so it would be truncated.

Yeah... Should I update the commit message and issue v2?

No need, I've already take this as-is.

#6 lore raw
In reply to Andy Shevchenko
Quoted 20 lines · On Thu, 16 Jan 2025 18:05:43 +0200 Andy Shevchenko…

GCC is not happy about the buffer size:

drivers/usb/core/hcd.c:441:48: error: ‘%s’ directive output may be truncated writing up to 64 bytes into a region of size between 35 and 99 [-Werror=format-truncation=]

  441 |                 snprintf (buf, sizeof buf, "%s %s %s", init_utsname()->sysname,
      |                                                ^~
  442 |                         init_utsname()->release, hcd->driver->description);
      |                         ~~~~~~~~~~~~~~~~~~~~~~~

Bump the size to get it enough for the possible strings.

---
 drivers/usb/core/hcd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/core/hcd.c +0 -0
index 0b2490347b9f..a75cf1f6d741 100644

drivers/usb/core/hcd.c ascii2desc(char const *s, u8 *buf, unsigned len) · line 418

Quoted 5 lines
drivers/usb/core/hcd.c +1 -1
@@ -415,7 +415,7 @@ ascii2desc(char const *s, u8 *buf, unsigned len)
 static unsigned
 rh_string(int id, struct usb_hcd const *hcd, u8 *data, unsigned len)
 {
-	char buf[100];
+	char buf[160];

Pretty pointless - look at ascii2desc() just above.
(Converts to LE i6-bit chars with a leading type+length.)
It gets truncated to 126 characters.
Indeed the entire snprintf() is pretty pointless given what happens to the data given that it is all strings.

Is the overall truncation even right?
The outer length is bounded to 254, but there may be fewer characters in the buffer because the buffer length itself might be smaller.
Seems a recipe for disaster.

David

Quoted 3 lines
drivers/usb/core/hcd.c +0 -0
 	char const *s;
 	static char const langids[4] = {4, USB_DT_STRING, 0x09, 0x04};