[dpdk-dev] [PATCH] kvargs: trim spaces at the beginning and end of key and values

Mesut Ali Ergin mesut.a.ergin at intel.com
Thu May 16 07:41:07 CEST 2019


When arguments delimited with RTE_KVARGS_PAIRS_DELIM happen to have
spaces before and after the delimiter, the rte_kvargs_process() can not
match them to the intended key. For example, the first argument below
triggers the support-multi-driver handler as expected, but the second
one silently fails to do so.

-w '84:00.0,support-multi-driver=1'
-w '84:00.0, support-multi-driver=1'

Signed-off-by: Mesut Ali Ergin <mesut.a.ergin at intel.com>
---
 lib/librte_kvargs/rte_kvargs.c | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)

diff --git a/lib/librte_kvargs/rte_kvargs.c b/lib/librte_kvargs/rte_kvargs.c
index f7030c6..1f45bc6 100644
--- a/lib/librte_kvargs/rte_kvargs.c
+++ b/lib/librte_kvargs/rte_kvargs.c
@@ -10,6 +10,32 @@
 
 #include "rte_kvargs.h"
 
+
+/* trim leading and trailing spaces */
+static char *
+trim_space(char *str)
+{
+	char *start, *end;
+
+	for (start = str; *start; start++) {
+		if (!isspace((unsigned char) start[0]))
+			break;
+	}
+
+	for (end = start + strlen(start); end > start + 1; end--) {
+		if (!isspace((unsigned char) end[-1]))
+			break;
+	}
+
+	*end = 0;
+
+	/* Shift from "start" to the beginning of the string */
+	if (start > str)
+		memmove(str, start, (end - start) + 1);
+
+	return str;
+}
+
 /*
  * Receive a string with a list of arguments following the pattern
  * key=value,key=value,... and insert them into the list.
@@ -38,8 +64,10 @@ rte_kvargs_tokenize(struct rte_kvargs *kvlist, const char *params)
 		if (i >= RTE_KVARGS_MAX)
 			return -1;
 
-		kvlist->pairs[i].key = strtok_r(str, RTE_KVARGS_KV_DELIM, &ctx2);
-		kvlist->pairs[i].value = strtok_r(NULL, RTE_KVARGS_KV_DELIM, &ctx2);
+		kvlist->pairs[i].key = trim_space(strtok_r(str,
+			RTE_KVARGS_KV_DELIM, &ctx2));
+		kvlist->pairs[i].value = trim_space(strtok_r(NULL,
+			RTE_KVARGS_KV_DELIM, &ctx2));
 		if (kvlist->pairs[i].key == NULL ||
 		    kvlist->pairs[i].value == NULL)
 			return -1;
-- 
2.7.4



More information about the dev mailing list