Skip to contentSkip to conversation

usb: Fix build with newer gcc

PATCH In review Eric Blake 2 participants 2 messages Jul 17, 2017

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH 1/1 usb: Fix build with newer gcc

gcc 7 is pickier about our sources:

hw/usb/bus.c: In function ‘usb_port_location’:
hw/usb/bus.c:410:66: error: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size between 0 and 15 [-Werror=format-truncation=]

         snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
                                                                  ^~
hw/usb/bus.c:410:9: note: ‘snprintf’ output between 3 and 28 bytes into a destination of size 16
         snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                  upstream->path, portnr);
                  ~~~~~~~~~~~~~~~~~~~~~~~

But we know that there are at most 5 levels of USB hubs, with at most two digits per level; that plus the separating dots means we use at most 15 bytes (including trailing NUL) of our 16-byte field. Adding an assertion to show gcc that we checked for truncation is enough to shut up the false-positive warning.

Inspired by an idea by Dr. David Alan Gilbert <[email protected]>.

---
 hw/usb/bus.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
hw/usb/bus.c +4 -2
index 5939b273b9..d910f849e7 100644
@@ -407,8 +407,10 @@ void usb_register_companion(const char *masterbus, USBPort *ports[],
 void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
 {
     if (upstream) {
-        snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
-                 upstream->path, portnr);
+        int l = snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
+                         upstream->path, portnr);
+        /* Max string is nn.nn.nn.nn.nn, which fits in 16 bytes */
+        assert(l < sizeof(downstream->path));
         downstream->hubcount = upstream->hubcount + 1;
     } else {
         snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
Signature · 1 line
2.13.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Eric Blake

gcc 7 is pickier about our sources:

hw/usb/bus.c: In function ‘usb_port_location’:
hw/usb/bus.c:410:66: error: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size between 0 and 15 [-Werror=format-truncation=]

          snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
                                                                   ^~
hw/usb/bus.c:410:9: note: ‘snprintf’ output between 3 and 28 bytes into a destination of size 16
          snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                   upstream->path, portnr);
                   ~~~~~~~~~~~~~~~~~~~~~~~
Quoted 7 lines

But we know that there are at most 5 levels of USB hubs, with at most two digits per level; that plus the separating dots means we use at most 15 bytes (including trailing NUL) of our 16-byte field. Adding an assertion to show gcc that we checked for truncation is enough to shut up the false-positive warning.

Inspired by an idea by Dr. David Alan Gilbert <[email protected]>.

elegant :)

Quoted 2 lines
Quoted 22 lines
---
  hw/usb/bus.c | 6 ++++--
  1 file changed, 4 insertions(+), 2 deletions(-)
hw/usb/bus.c +4 -2
index 5939b273b9..d910f849e7 100644
@@ -407,8 +407,10 @@ void usb_register_companion(const char *masterbus, USBPort *ports[],
  void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
  {
      if (upstream) {
-        snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
-                 upstream->path, portnr);
+        int l = snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
+                         upstream->path, portnr);
+        /* Max string is nn.nn.nn.nn.nn, which fits in 16 bytes */
+        assert(l < sizeof(downstream->path));
          downstream->hubcount = upstream->hubcount + 1;
      } else {
          snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);