DPDK patches and discussions
 help / color / mirror / Atom feed
* [dpdk-dev] [PATCH] kvargs: trim spaces at the beginning and end of key and values
@ 2019-05-16  5:41 Mesut Ali Ergin
  2019-05-16  5:41 ` Mesut Ali Ergin
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Mesut Ali Ergin @ 2019-05-16  5:41 UTC (permalink / raw)
  To: olivier.matz; +Cc: dev, Mesut Ali Ergin

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@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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [dpdk-dev] [PATCH] kvargs: trim spaces at the beginning and end of key and values
  2019-05-16  5:41 [dpdk-dev] [PATCH] kvargs: trim spaces at the beginning and end of key and values Mesut Ali Ergin
@ 2019-05-16  5:41 ` Mesut Ali Ergin
  2019-05-16 16:36 ` Stephen Hemminger
  2019-07-01 13:10 ` Olivier Matz
  2 siblings, 0 replies; 6+ messages in thread
From: Mesut Ali Ergin @ 2019-05-16  5:41 UTC (permalink / raw)
  To: olivier.matz; +Cc: dev, Mesut Ali Ergin

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@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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [dpdk-dev] [PATCH] kvargs: trim spaces at the beginning and end of key and values
  2019-05-16  5:41 [dpdk-dev] [PATCH] kvargs: trim spaces at the beginning and end of key and values Mesut Ali Ergin
  2019-05-16  5:41 ` Mesut Ali Ergin
@ 2019-05-16 16:36 ` Stephen Hemminger
  2019-05-16 20:55   ` Ergin, Mesut A
  2019-07-01 13:10 ` Olivier Matz
  2 siblings, 1 reply; 6+ messages in thread
From: Stephen Hemminger @ 2019-05-16 16:36 UTC (permalink / raw)
  To: Mesut Ali Ergin; +Cc: olivier.matz, dev

On Wed, 15 May 2019 22:41:07 -0700
Mesut Ali Ergin <mesut.a.ergin@intel.com> wrote:

> +/* 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;
> +	}
> +

Why not use existing string functions like strspn?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [dpdk-dev] [PATCH] kvargs: trim spaces at the beginning and end of key and values
  2019-05-16 16:36 ` Stephen Hemminger
@ 2019-05-16 20:55   ` Ergin, Mesut A
  2019-05-16 23:18     ` Stephen Hemminger
  0 siblings, 1 reply; 6+ messages in thread
From: Ergin, Mesut A @ 2019-05-16 20:55 UTC (permalink / raw)
  To: Stephen Hemminger; +Cc: olivier.matz, dev



> -----Original Message-----
> From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> Sent: Thursday, May 16, 2019 9:37 AM
> To: Ergin, Mesut A <mesut.a.ergin@intel.com>
> Cc: olivier.matz@6wind.com; dev@dpdk.org
> Subject: Re: [dpdk-dev] [PATCH] kvargs: trim spaces at the beginning and end of
> key and values
> 
> On Wed, 15 May 2019 22:41:07 -0700
> Mesut Ali Ergin <mesut.a.ergin@intel.com> wrote:
> 
> > +/* 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;
> > +	}
> > +
> 
> Why not use existing string functions like strspn?

Had no particular reason not to use strspn. This was in use/tested in two apps shipping already, and I took the easy route. I could modify if you think it will be better.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [dpdk-dev] [PATCH] kvargs: trim spaces at the beginning and end of key and values
  2019-05-16 20:55   ` Ergin, Mesut A
@ 2019-05-16 23:18     ` Stephen Hemminger
  0 siblings, 0 replies; 6+ messages in thread
From: Stephen Hemminger @ 2019-05-16 23:18 UTC (permalink / raw)
  To: Ergin, Mesut A; +Cc: olivier.matz, dev

On Thu, 16 May 2019 20:55:10 +0000
"Ergin, Mesut A" <mesut.a.ergin@intel.com> wrote:

> > -----Original Message-----
> > From: Stephen Hemminger [mailto:stephen@networkplumber.org]
> > Sent: Thursday, May 16, 2019 9:37 AM
> > To: Ergin, Mesut A <mesut.a.ergin@intel.com>
> > Cc: olivier.matz@6wind.com; dev@dpdk.org
> > Subject: Re: [dpdk-dev] [PATCH] kvargs: trim spaces at the beginning and end of
> > key and values
> > 
> > On Wed, 15 May 2019 22:41:07 -0700
> > Mesut Ali Ergin <mesut.a.ergin@intel.com> wrote:
> >   
> > > +/* 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;
> > > +	}
> > > +  
> > 
> > Why not use existing string functions like strspn?  
> 
> Had no particular reason not to use strspn. This was in use/tested in two apps shipping already, and I took the easy route. I could modify if you think it will be better.

writing your own string handling code is often a source of bugs

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [dpdk-dev] [PATCH] kvargs: trim spaces at the beginning and end of key and values
  2019-05-16  5:41 [dpdk-dev] [PATCH] kvargs: trim spaces at the beginning and end of key and values Mesut Ali Ergin
  2019-05-16  5:41 ` Mesut Ali Ergin
  2019-05-16 16:36 ` Stephen Hemminger
@ 2019-07-01 13:10 ` Olivier Matz
  2 siblings, 0 replies; 6+ messages in thread
From: Olivier Matz @ 2019-07-01 13:10 UTC (permalink / raw)
  To: Mesut Ali Ergin; +Cc: dev

Hi,

On Wed, May 15, 2019 at 10:41:07PM -0700, Mesut Ali Ergin wrote:
> 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@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;

Having a space in the kwvargs strings like in your example
(-w '84:00.0, support-multi-driver=1') is wrong. I don't think we should
fix this in kvargs library.

I you feel it is necessary, we could potentially add a comment in the API
description to highlight this.


Thanks,
Olivier

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-07-01 13:10 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-16  5:41 [dpdk-dev] [PATCH] kvargs: trim spaces at the beginning and end of key and values Mesut Ali Ergin
2019-05-16  5:41 ` Mesut Ali Ergin
2019-05-16 16:36 ` Stephen Hemminger
2019-05-16 20:55   ` Ergin, Mesut A
2019-05-16 23:18     ` Stephen Hemminger
2019-07-01 13:10 ` Olivier Matz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).