04/10/2018 11:44, Gaëtan Rivet: > On Thu, Oct 04, 2018 at 01:10:46AM +0200, Thomas Monjalon wrote: > > --- a/lib/librte_eal/common/eal_common_dev.c > > +++ b/lib/librte_eal/common/eal_common_dev.c > > @@ -129,46 +129,61 @@ int rte_eal_dev_detach(struct rte_device *dev) > > > > int > > rte_eal_hotplug_add(const char *busname, const char *devname, > > - const char *devargs) > > + const char *drvargs) > > { > > - struct rte_bus *bus; > > - struct rte_device *dev; > > - struct rte_devargs *da; > > int ret; > > + char *devargs = NULL; > > + int size, length = -1; > > > > - bus = rte_bus_find_by_name(busname); > > - if (bus == NULL) { > > - RTE_LOG(ERR, EAL, "Cannot find bus (%s)\n", busname); > > - return -ENOENT; > > - } > > + do { /* 2 iterations: first is to know string length */ > > + size = length + 1; > > + length = snprintf(devargs, size, "%s:%s,%s", busname, devname, > > drvargs); > > + if (length >= size) > > + devargs = malloc(length + 1); > > + if (devargs == NULL) > > + return -ENOMEM; > > + } while (size == 0); > > I don't see a good reason for writing it this way. > You use an additional state (size) and complicate something that is > pretty straightforward. To make sure no error was written here, I had to > do step-by-step careful reading, this should not be required by clean > code. > > You should remove this loop, which then allow removing size, and forces using > simple code-flow.
The only reason for this loop is to avoid duplicating the snprintf format in two calls. It could be replaced by this: length = snprintf(devargs, 0, "%s:%s,%s", busname, devname, drvargs); if (length < 0) return -EINVAL; devargs = malloc(length + 1); if (devargs == NULL) return -ENOMEM; snprintf(devargs, length + 1, "%s:%s,%s", busname, devname, drvargs); Any preference?