@samitouri / QOSamiQemu / commits / 4dcb61eb4f

hw/pci: handle missing bus in prop_pci_busnr_get

When called on an unrealized device (e.g. from qmp_qom_list_properties), pci_get_bus() returns NULL since the device has no parent bus. Check for this to avoid a NULL dereference in pci_bus_num(). Fixes: df9ac7254fd9 ("hw/pci: Add a busnr property to pci_props and use for acpi/gi") Reviewed-by: Daniel P. Berrangé <berrange@redhat.com> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>

Marc-André Lureau committed Apr 24, 2026 at 19:04 UTC 4dcb61eb4fc0844242c6e467fe4098fa5cd308cc
1 file changed +9 -2
hw/pci/pci.c
+9 -2
@@ -64,10 +64,17 @@ static void pcibus_reset_hold(Object *obj, ResetType type);
64 static bool pcie_has_upstream_port(PCIDevice *dev);
65
66 static void prop_pci_busnr_get(Object *obj, Visitor *v, const char *name,
67 - void *opaque, Error **errp)
67 + void *opaque, Error **errp)
68 {
69 - uint8_t busnr = pci_dev_bus_num(PCI_DEVICE(obj));
69 + PCIDevice *dev = PCI_DEVICE(obj);
70 + PCIBus *bus = pci_get_bus(dev);
71 + uint8_t busnr;
72
73 + if (!bus) {
74 + error_setg(errp, "device not attached to a PCI bus");
75 + return;
76 + }
77 + busnr = pci_bus_num(bus);
78 visit_type_uint8(v, name, &busnr, errp);
79 }
80