Skip to contentSkip to conversation

hw/pci: Add parenthesis to PCI_BUILD_BDF macro

PATCH In review Roque Arcudia Hernandez 2 participants 2 messages Nov 1, 2024 – Nov 3, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/pci: Add parenthesis to PCI_BUILD_BDF macro

The bus parameter in the macro PCI_BUILD_BDF is not surrounded by parenthesis. This can create a compile error when warnings are treated as errors or can potentially create runtime errors due to the operator precedence.

For instance:

file.c:x:32: error: suggest parentheses around '-' inside '<<' [-Werror=parentheses]
171 | uint16_t bdf = PCI_BUILD_BDF(a - b, sdev->devfn);
| ~~^~~
include/hw/pci/pci.h:19:41: note: in definition of macro 'PCI_BUILD_BDF'

    19 | #define PCI_BUILD_BDF(bus, devfn)     ((bus << 8) | (devfn))
       |                                         ^~~

cc1: all warnings being treated as errors

---
 include/hw/pci/pci.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/pci/pci.h +1 -1
index 35d4fe0bbf..1daec974da 100644
@@ -16,7 +16,7 @@ extern bool pci_available;
 #define PCI_BUS_NUM(x)          (((x) >> 8) & 0xff)
 #define PCI_SLOT(devfn)         (((devfn) >> 3) & 0x1f)
 #define PCI_FUNC(devfn)         ((devfn) & 0x07)
-#define PCI_BUILD_BDF(bus, devfn)     ((bus << 8) | (devfn))
+#define PCI_BUILD_BDF(bus, devfn)     (((bus) << 8) | (devfn))
 #define PCI_BDF_TO_DEVFN(x)     ((x) & 0xff)
 #define PCI_BUS_MAX             256
 #define PCI_DEVFN_MAX           256
Signature · 1 line
2.47.0.163.g1226f6d8fa-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 28 lines · Roque Arcudia Hernandez

The bus parameter in the macro PCI_BUILD_BDF is not surrounded by parenthesis. This can create a compile error when warnings are treated as errors or can potentially create runtime errors due to the operator precedence.

For instance:

file.c:x:32: error: suggest parentheses around '-' inside '<<' [-Werror=parentheses]

    171 | uint16_t bdf = PCI_BUILD_BDF(a - b, sdev->devfn);
        |                              ~~^~~
  include/hw/pci/pci.h:19:41: note: in definition of macro
  'PCI_BUILD_BDF'
     19 | #define PCI_BUILD_BDF(bus, devfn)     ((bus << 8) | (devfn))
        |                                         ^~~

cc1: all warnings being treated as errors

---
  include/hw/pci/pci.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/hw/pci/pci.h +0 -0
index 35d4fe0bbf..1daec974da 100644

include/hw/pci/pci.h extern bool pci_available; · line 22

Quoted 8 lines
include/hw/pci/pci.h +1 -1
@@ -16,7 +16,7 @@ extern bool pci_available;
  #define PCI_BUS_NUM(x)          (((x) >> 8) & 0xff)
  #define PCI_SLOT(devfn)         (((devfn) >> 3) & 0x1f)
  #define PCI_FUNC(devfn)         ((devfn) & 0x07)
-#define PCI_BUILD_BDF(bus, devfn)     ((bus << 8) | (devfn))
+#define PCI_BUILD_BDF(bus, devfn)     (((bus) << 8) | (devfn))
  #define PCI_BDF_TO_DEVFN(x)     ((x) & 0xff)
  #define PCI_BUS_MAX             256
  #define PCI_DEVFN_MAX           256