eal: fix leak on hotplug parsing error
[dpdk.git] / lib / librte_eal / common / eal_common_dev.c
index d74f978..fc8a4d2 100644 (file)
@@ -66,6 +66,7 @@ static int cmp_dev_name(const struct rte_device *dev, const void *_name)
 
 int rte_eal_dev_attach(const char *name, const char *devargs)
 {
+       struct rte_bus *bus;
        int ret;
 
        if (name == NULL || devargs == NULL) {
@@ -73,9 +74,18 @@ int rte_eal_dev_attach(const char *name, const char *devargs)
                return -EINVAL;
        }
 
-       ret = rte_eal_hotplug_add("pci", name, devargs);
-       if (ret != -EINVAL)
-               return ret;
+       bus = rte_bus_find_by_device_name(name);
+       if (bus == NULL) {
+               RTE_LOG(ERR, EAL, "Unable to find a bus for the device '%s'\n",
+                       name);
+               return -EINVAL;
+       }
+       if (strcmp(bus->name, "pci") == 0)
+               return rte_eal_hotplug_add("pci", name, devargs);
+       if (strcmp(bus->name, "vdev") != 0) {
+               RTE_LOG(ERR, EAL, "Device attach is only supported for PCI and vdev devices.\n");
+               return -ENOTSUP;
+       }
 
        /*
         * If we haven't found a bus device the user meant to "hotplug" a
@@ -123,16 +133,13 @@ full_dev_name(const char *bus, const char *dev, const char *args)
        char *name;
        size_t len;
 
-       len = strlen(bus) + 1 +
-             strlen(dev) + 1 +
-             strlen(args) + 1;
+       len = snprintf(NULL, 0, "%s:%s,%s", bus, dev, args);
        name = calloc(1, len);
        if (name == NULL) {
                RTE_LOG(ERR, EAL, "Could not allocate full device name\n");
                return NULL;
        }
-       snprintf(name, len, "%s:%s,%s", bus, dev,
-                args ? args : "");
+       snprintf(name, len, "%s:%s,%s", bus, dev, args);
        return name;
 }
 
@@ -197,7 +204,10 @@ int rte_eal_hotplug_add(const char *busname, const char *devname,
        return 0;
 
 err_devarg:
-       rte_eal_devargs_remove(busname, devname);
+       if (rte_eal_devargs_remove(busname, devname)) {
+               free(da->args);
+               free(da);
+       }
 err_name:
        free(name);
        return ret;