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: df9ac7254f ("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>
This commit is contained in:
Marc-André Lureau
2026-04-24 19:04:35 +04:00
parent 29c042c6e9
commit 4dcb61eb4f

View File

@@ -64,10 +64,17 @@ static void pcibus_reset_hold(Object *obj, ResetType type);
static bool pcie_has_upstream_port(PCIDevice *dev);
static void prop_pci_busnr_get(Object *obj, Visitor *v, const char *name,
void *opaque, Error **errp)
void *opaque, Error **errp)
{
uint8_t busnr = pci_dev_bus_num(PCI_DEVICE(obj));
PCIDevice *dev = PCI_DEVICE(obj);
PCIBus *bus = pci_get_bus(dev);
uint8_t busnr;
if (!bus) {
error_setg(errp, "device not attached to a PCI bus");
return;
}
busnr = pci_bus_num(bus);
visit_type_uint8(v, name, &busnr, errp);
}