From: Chris Wright <[EMAIL PROTECTED]>

The pci_register_device() call in PCI nic initialization routines can
fail.  Handle this failure and propagate a meaningful error message to
the user instead of generating a SEGV.

Cc: Marcelo Tosatti <[EMAIL PROTECTED]>
Signed-off-by: Chris Wright <[EMAIL PROTECTED]>
Signed-off-by: Avi Kivity <[EMAIL PROTECTED]>

diff --git a/qemu/hw/e1000.c b/qemu/hw/e1000.c
index e8462bd..0728539 100644
--- a/qemu/hw/e1000.c
+++ b/qemu/hw/e1000.c
@@ -963,6 +963,9 @@ pci_e1000_init(PCIBus *bus, NICInfo *nd, int devfn)
     d = (E1000State *)pci_register_device(bus, "e1000",
                 sizeof(E1000State), devfn, NULL, NULL);
 
+    if (!d)
+       return NULL;
+
     pci_conf = d->dev.config;
     memset(pci_conf, 0, 256);
 
diff --git a/qemu/hw/eepro100.c b/qemu/hw/eepro100.c
index a5ea544..ccb5f8b 100644
--- a/qemu/hw/eepro100.c
+++ b/qemu/hw/eepro100.c
@@ -1753,6 +1753,8 @@ static PCIDevice *nic_init(PCIBus * bus, NICInfo * nd,
     d = (PCIEEPRO100State *) pci_register_device(bus, name,
                                                  sizeof(PCIEEPRO100State), -1,
                                                  NULL, NULL);
+    if (!d)
+        return NULL;
 
     s = &d->eepro100;
     s->device = device;
diff --git a/qemu/hw/ne2000.c b/qemu/hw/ne2000.c
index 604fc13..ddc59b5 100644
--- a/qemu/hw/ne2000.c
+++ b/qemu/hw/ne2000.c
@@ -796,6 +796,9 @@ PCIDevice *pci_ne2000_init(PCIBus *bus, NICInfo *nd, int 
devfn)
                                               "NE2000", sizeof(PCINE2000State),
                                               devfn,
                                               NULL, NULL);
+    if (!d)
+       return NULL;
+
     pci_conf = d->dev.config;
     pci_conf[0x00] = 0xec; // Realtek 8029
     pci_conf[0x01] = 0x10;
diff --git a/qemu/hw/pci.c b/qemu/hw/pci.c
index b8f4fbb..a23a466 100644
--- a/qemu/hw/pci.c
+++ b/qemu/hw/pci.c
@@ -696,6 +696,12 @@ PCIDevice *pci_nic_init(PCIBus *bus, NICInfo *nd, int 
devfn)
         fprintf(stderr, "qemu: Unsupported NIC: %s\n", nd->model);
         return NULL;
     }
+
+    if (!pci_dev) {
+        fprintf(stderr, "qemu: Unable to initialze NIC: %s\n", nd->model);
+        return NULL;
+    }
+
     nd->devfn = pci_dev->devfn;
     return pci_dev;
 }
diff --git a/qemu/hw/pcnet.c b/qemu/hw/pcnet.c
index 5ecac60..75a94c0 100644
--- a/qemu/hw/pcnet.c
+++ b/qemu/hw/pcnet.c
@@ -1970,6 +1970,8 @@ PCIDevice *pci_pcnet_init(PCIBus *bus, NICInfo *nd, int 
devfn)
 
     d = (PCNetState *)pci_register_device(bus, "PCNet", sizeof(PCNetState),
                                           devfn, NULL, NULL);
+    if (!d)
+       return NULL;
 
     pci_conf = d->dev.config;
 
diff --git a/qemu/hw/rtl8139.c b/qemu/hw/rtl8139.c
index acac22f..e4df58f 100644
--- a/qemu/hw/rtl8139.c
+++ b/qemu/hw/rtl8139.c
@@ -3411,6 +3411,9 @@ PCIDevice *pci_rtl8139_init(PCIBus *bus, NICInfo *nd, int 
devfn)
                                               "RTL8139", 
sizeof(PCIRTL8139State),
                                               devfn,
                                               NULL, NULL);
+    if (!d)
+       return NULL;
+
     pci_conf = d->dev.config;
     pci_conf[0x00] = 0xec; /* Realtek 8139 */
     pci_conf[0x01] = 0x10;
diff --git a/qemu/hw/virtio-net.c b/qemu/hw/virtio-net.c
index 3d54c4e..f727b14 100644
--- a/qemu/hw/virtio-net.c
+++ b/qemu/hw/virtio-net.c
@@ -292,6 +292,8 @@ PCIDevice *virtio_net_init(PCIBus *bus, NICInfo *nd, int 
devfn)
                                     0, VIRTIO_ID_NET,
                                     0x02, 0x00, 0x00,
                                     6, sizeof(VirtIONet));
+    if (!n)
+       return NULL;
 
     n->vdev.update_config = virtio_net_update_config;
     n->vdev.get_features = virtio_net_get_features;
diff --git a/qemu/hw/virtio.c b/qemu/hw/virtio.c
index 9100bb1..6a50001 100644
--- a/qemu/hw/virtio.c
+++ b/qemu/hw/virtio.c
@@ -408,6 +408,9 @@ VirtIODevice *virtio_init_pci(PCIBus *bus, const char *name,
 
     pci_dev = pci_register_device(bus, name, struct_size,
                                  -1, NULL, NULL);
+    if (!pci_dev)
+       return NULL;
+
     vdev = to_virtio_device(pci_dev);
 
     vdev->status = 0;

-------------------------------------------------------------------------
This SF.net email is sponsored by the 2008 JavaOne(SM) Conference 
Don't miss this year's exciting event. There's still time to save $100. 
Use priority code J8TL2D2. 
http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone
_______________________________________________
kvm-commits mailing list
kvm-commits@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/kvm-commits

Reply via email to