diff mbox

[dpdk-dev,2/2] devargs: remove limit on parameters length

Message ID 1420635809-30976-3-git-send-email-david.marchand@6wind.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

David Marchand Jan. 7, 2015, 1:03 p.m. UTC
As far as I know, there is no reason why we should have a limit on the length of
parameters that can be given for a device.
Remove this limit by using dynamic allocations.

Signed-off-by: David Marchand <david.marchand@6wind.com>
---
 lib/librte_eal/common/eal_common_devargs.c  |   26 +++++++++++++++++---------
 lib/librte_eal/common/include/rte_devargs.h |    4 ++--
 2 files changed, 19 insertions(+), 11 deletions(-)

Comments

Stephen Hemminger Jan. 7, 2015, 10:59 p.m. UTC | #1
On Wed,  7 Jan 2015 14:03:29 +0100
David Marchand <david.marchand@6wind.com> wrote:

> +	buf = strdup(devargs_str);
> +	if (buf == NULL) {
> +		RTE_LOG(ERR, EAL, "cannot allocate temp memory for devargs\n");
> +		goto fail;
> +	}
> +

If string is only used in same function you might consider using strdupa() which avoids
worrying about freeing in error paths.
David Marchand Jan. 8, 2015, 8:19 a.m. UTC | #2
On Wed, Jan 7, 2015 at 11:59 PM, Stephen Hemminger <
stephen@networkplumber.org> wrote:

> On Wed,  7 Jan 2015 14:03:29 +0100
> David Marchand <david.marchand@6wind.com> wrote:
>
> > +     buf = strdup(devargs_str);
> > +     if (buf == NULL) {
> > +             RTE_LOG(ERR, EAL, "cannot allocate temp memory for
> devargs\n");
> > +             goto fail;
> > +     }
> > +
>
> If string is only used in same function you might consider using strdupa()
> which avoids
> worrying about freeing in error paths.
>

Hum, why not.
My only concern is strdupa() availability on BSD.
diff mbox

Patch

diff --git a/lib/librte_eal/common/eal_common_devargs.c b/lib/librte_eal/common/eal_common_devargs.c
index 8c9b31a..3aace08 100644
--- a/lib/librte_eal/common/eal_common_devargs.c
+++ b/lib/librte_eal/common/eal_common_devargs.c
@@ -49,17 +49,10 @@  int
 rte_eal_devargs_add(enum rte_devtype devtype, const char *devargs_str)
 {
 	struct rte_devargs *devargs = NULL;
-	char buf[RTE_DEVARGS_LEN];
+	char *buf = NULL;
 	char *sep;
 	int ret;
 
-	ret = snprintf(buf, sizeof(buf), "%s", devargs_str);
-	if (ret < 0 || ret >= (int)sizeof(buf)) {
-		RTE_LOG(ERR, EAL, "user device args too large: <%s>\n",
-			devargs_str);
-		goto fail;
-	}
-
 	/* use malloc instead of rte_malloc as it's called early at init */
 	devargs = malloc(sizeof(*devargs));
 	if (devargs == NULL) {
@@ -69,11 +62,21 @@  rte_eal_devargs_add(enum rte_devtype devtype, const char *devargs_str)
 	memset(devargs, 0, sizeof(*devargs));
 	devargs->type = devtype;
 
+	buf = strdup(devargs_str);
+	if (buf == NULL) {
+		RTE_LOG(ERR, EAL, "cannot allocate temp memory for devargs\n");
+		goto fail;
+	}
+
 	/* set the first ',' to '\0' to split name and arguments */
 	sep = strchr(buf, ',');
 	if (sep != NULL) {
 		sep[0] = '\0';
-		snprintf(devargs->args, sizeof(devargs->args), "%s", sep + 1);
+		devargs->args = strdup(sep + 1);
+		if (devargs->args == NULL) {
+			RTE_LOG(ERR, EAL, "cannot allocate for devargs args\n");
+			goto fail;
+		}
 	}
 
 	switch (devargs->type) {
@@ -97,10 +100,15 @@  rte_eal_devargs_add(enum rte_devtype devtype, const char *devargs_str)
 		break;
 	}
 
+	free(buf);
 	TAILQ_INSERT_TAIL(&devargs_list, devargs, next);
 	return 0;
 
 fail:
+	if (devargs->args)
+		free(devargs->args);
+	if (buf)
+		free(buf);
 	if (devargs)
 		free(devargs);
 	return -1;
diff --git a/lib/librte_eal/common/include/rte_devargs.h b/lib/librte_eal/common/include/rte_devargs.h
index 9f9c98f..996e180 100644
--- a/lib/librte_eal/common/include/rte_devargs.h
+++ b/lib/librte_eal/common/include/rte_devargs.h
@@ -88,8 +88,8 @@  struct rte_devargs {
 			char drv_name[32];
 		} virtual;
 	};
-#define RTE_DEVARGS_LEN 256
-	char args[RTE_DEVARGS_LEN]; /**< Arguments string as given by user. */
+	/** Arguments string as given by user. */
+	char *args;
 };
 
 /** user device double-linked queue type definition */