Commit 4dcb61eb4f for qemu.org

commit 4dcb61eb4fc0844242c6e467fe4098fa5cd308cc
Author: Marc-André Lureau <marcandre.lureau@redhat.com>
Date:   Fri Apr 24 19:04:35 2026 +0400

    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>

diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index 4298adf5a0..cec065d108 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -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);
 }