Commit aff427a1ca09945082a4ec21aee2960306800fb0

Authored by Chris Wright
Committed by Anthony Liguori
1 parent e6a6dfe4

Pci nic: pci_register_device can fail

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 <mtosatti@redhat.com>
Signed-off-by: Chris Wright <chrisw@sous-sol.org>
Signed-off-by: Avi Kivity <avi@qumranet.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
hw/e1000.c
@@ -1063,6 +1063,9 @@ pci_e1000_init(PCIBus *bus, NICInfo *nd, int devfn) @@ -1063,6 +1063,9 @@ pci_e1000_init(PCIBus *bus, NICInfo *nd, int devfn)
1063 d = (E1000State *)pci_register_device(bus, "e1000", 1063 d = (E1000State *)pci_register_device(bus, "e1000",
1064 sizeof(E1000State), devfn, NULL, NULL); 1064 sizeof(E1000State), devfn, NULL, NULL);
1065 1065
  1066 + if (!d)
  1067 + return NULL;
  1068 +
1066 pci_conf = d->dev.config; 1069 pci_conf = d->dev.config;
1067 memset(pci_conf, 0, 256); 1070 memset(pci_conf, 0, 256);
1068 1071
hw/eepro100.c
@@ -1739,6 +1739,9 @@ static PCIDevice *nic_init(PCIBus * bus, NICInfo * nd, uint32_t device) @@ -1739,6 +1739,9 @@ static PCIDevice *nic_init(PCIBus * bus, NICInfo * nd, uint32_t device)
1739 d = (PCIEEPRO100State *) pci_register_device(bus, nd->model, 1739 d = (PCIEEPRO100State *) pci_register_device(bus, nd->model,
1740 sizeof(PCIEEPRO100State), -1, 1740 sizeof(PCIEEPRO100State), -1,
1741 NULL, NULL); 1741 NULL, NULL);
  1742 + if (!d)
  1743 + return NULL;
  1744 +
1742 d->dev.unregister = pci_nic_uninit; 1745 d->dev.unregister = pci_nic_uninit;
1743 1746
1744 s = &d->eepro100; 1747 s = &d->eepro100;
hw/ne2000.c
@@ -810,6 +810,9 @@ PCIDevice *pci_ne2000_init(PCIBus *bus, NICInfo *nd, int devfn) @@ -810,6 +810,9 @@ PCIDevice *pci_ne2000_init(PCIBus *bus, NICInfo *nd, int devfn)
810 "NE2000", sizeof(PCINE2000State), 810 "NE2000", sizeof(PCINE2000State),
811 devfn, 811 devfn,
812 NULL, NULL); 812 NULL, NULL);
  813 + if (!d)
  814 + return NULL;
  815 +
813 pci_conf = d->dev.config; 816 pci_conf = d->dev.config;
814 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_REALTEK); 817 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_REALTEK);
815 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_REALTEK_8029); 818 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_REALTEK_8029);
hw/pcnet.c
@@ -2023,7 +2023,11 @@ PCIDevice *pci_pcnet_init(PCIBus *bus, NICInfo *nd, int devfn) @@ -2023,7 +2023,11 @@ PCIDevice *pci_pcnet_init(PCIBus *bus, NICInfo *nd, int devfn)
2023 2023
2024 d = (PCNetState *)pci_register_device(bus, "PCNet", sizeof(PCNetState), 2024 d = (PCNetState *)pci_register_device(bus, "PCNet", sizeof(PCNetState),
2025 devfn, NULL, NULL); 2025 devfn, NULL, NULL);
  2026 + if (!d)
  2027 + return NULL;
  2028 +
2026 d->dev.unregister = pci_pcnet_uninit; 2029 d->dev.unregister = pci_pcnet_uninit;
  2030 +
2027 pci_conf = d->dev.config; 2031 pci_conf = d->dev.config;
2028 2032
2029 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_AMD); 2033 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_AMD);
hw/rtl8139.c
@@ -3451,7 +3451,11 @@ PCIDevice *pci_rtl8139_init(PCIBus *bus, NICInfo *nd, int devfn) @@ -3451,7 +3451,11 @@ PCIDevice *pci_rtl8139_init(PCIBus *bus, NICInfo *nd, int devfn)
3451 "RTL8139", sizeof(PCIRTL8139State), 3451 "RTL8139", sizeof(PCIRTL8139State),
3452 devfn, 3452 devfn,
3453 NULL, NULL); 3453 NULL, NULL);
  3454 + if (!d)
  3455 + return NULL;
  3456 +
3454 d->dev.unregister = pci_rtl8139_uninit; 3457 d->dev.unregister = pci_rtl8139_uninit;
  3458 +
3455 pci_conf = d->dev.config; 3459 pci_conf = d->dev.config;
3456 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_REALTEK); 3460 pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_REALTEK);
3457 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_REALTEK_8139); 3461 pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_REALTEK_8139);