[libvirt] [PATCH 3/6] virPCIDevice: Make @name dynamically allocated

Michal Privoznik mprivozn at redhat.com
Wed Jul 31 08:32:21 UTC 2019


In near future, the length restriction of PCI domain is going to
be lifted. This means that our assumption that PCI address is 13
bytes long is no longer true. We can avoid this problem by making
@name dynamically allocated and thus not bother with actual
length of stringified PCI address.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/util/virpci.c | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)

diff --git a/src/util/virpci.c b/src/util/virpci.c
index 1a3d316399..6d8ddc3577 100644
--- a/src/util/virpci.c
+++ b/src/util/virpci.c
@@ -44,7 +44,6 @@ VIR_LOG_INIT("util.pci");
 
 #define PCI_SYSFS "/sys/bus/pci/"
 #define PCI_ID_LEN 10   /* "XXXX XXXX" */
-#define PCI_ADDR_LEN 13 /* "XXXX:XX:XX.X" */
 
 VIR_ENUM_IMPL(virPCIELinkSpeed,
               VIR_PCIE_LINK_SPEED_LAST,
@@ -69,7 +68,7 @@ VIR_ENUM_IMPL(virPCIHeader,
 struct _virPCIDevice {
     virPCIDeviceAddress address;
 
-    char          name[PCI_ADDR_LEN]; /* domain:bus:slot.function */
+    char          *name;              /* domain:bus:slot.function */
     char          id[PCI_ID_LEN];     /* product vendor */
     char          *path;
 
@@ -1761,13 +1760,11 @@ virPCIDeviceNew(unsigned int domain,
     dev->address.slot = slot;
     dev->address.function = function;
 
-    if (snprintf(dev->name, sizeof(dev->name), "%.4x:%.2x:%.2x.%.1x",
-                 domain, bus, slot, function) >= sizeof(dev->name)) {
-        virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("dev->name buffer overflow: %.4x:%.2x:%.2x.%.1x"),
-                       domain, bus, slot, function);
+    if (virAsprintf(&dev->name,
+                    "%.4x:%.2x:%.2x.%.1x",
+                    domain, bus, slot, function) < 0)
         return NULL;
-    }
+
     if (virAsprintf(&dev->path, PCI_SYSFS "devices/%s/config",
                     dev->name) < 0)
         return NULL;
@@ -1816,7 +1813,8 @@ virPCIDeviceCopy(virPCIDevicePtr dev)
     *copy = *dev;
     copy->path = NULL;
     copy->used_by_drvname = copy->used_by_domname = NULL;
-    if (VIR_STRDUP(copy->path, dev->path) < 0 ||
+    if (VIR_STRDUP(copy->name, dev->name) < 0 ||
+        VIR_STRDUP(copy->path, dev->path) < 0 ||
         VIR_STRDUP(copy->used_by_drvname, dev->used_by_drvname) < 0 ||
         VIR_STRDUP(copy->used_by_domname, dev->used_by_domname) < 0) {
         goto error;
@@ -1835,6 +1833,7 @@ virPCIDeviceFree(virPCIDevicePtr dev)
     if (!dev)
         return;
     VIR_DEBUG("%s %s: freeing", dev->id, dev->name);
+    VIR_FREE(dev->name);
     VIR_FREE(dev->path);
     VIR_FREE(dev->used_by_drvname);
     VIR_FREE(dev->used_by_domname);
-- 
2.21.0




More information about the libvir-list mailing list