[dpdk-dev,v1] lib/cmdline: init parse result memory

Message ID 20171208070244.24094-1-xuemingl@mellanox.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/Intel-compilation success Compilation OK

Commit Message

Xueming Li Dec. 8, 2017, 7:02 a.m. UTC
  Initialize binary result memory before parsing to avoid garbage in
parsing result.

Signed-off-by: Xueming Li <xuemingl@mellanox.com>
---
 lib/librte_cmdline/cmdline_parse.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
  

Comments

Adrien Mazarguil Dec. 8, 2017, 12:27 p.m. UTC | #1
On Fri, Dec 08, 2017 at 03:02:44PM +0800, Xueming Li wrote:
> Initialize binary result memory before parsing to avoid garbage in
> parsing result.
> 
> Signed-off-by: Xueming Li <xuemingl@mellanox.com>

Since you chose to move the break statement, maybe the original commit
mentioned in my previous message (9b3fbb051d2e "cmdline: fix parsing") can
be reverted afterward? I think it makes tmp_result redundant.

Wenzhuo, as the author of that commit, can you confirm?

Olivier, no problem with breaking the loop immediately after the first
successful match_inst() call instead of the last one? (I don't see why it
would be an issue but I may have missed something)

> ---
>  lib/librte_cmdline/cmdline_parse.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/lib/librte_cmdline/cmdline_parse.c b/lib/librte_cmdline/cmdline_parse.c
> index 3e12ee54f..4072103f2 100644
> --- a/lib/librte_cmdline/cmdline_parse.c
> +++ b/lib/librte_cmdline/cmdline_parse.c
> @@ -168,6 +168,9 @@ match_inst(cmdline_parse_inst_t *inst, const char *buf,
>  	int n = 0;
>  	struct cmdline_token_hdr token_hdr;
>  
> +	if (resbuf != NULL)
> +		memset(resbuf, 0, resbuf_size);
> +
>  	/* check if we match all tokens of inst */
>  	while (!nb_match_token || i < nb_match_token) {
>  		token_p = get_token(inst, i);
> @@ -338,8 +341,8 @@ cmdline_parse(struct cmdline *cl, const char * buf)
>  					err = CMDLINE_PARSE_AMBIGUOUS;
>  					f=NULL;
>  					debug_printf("Ambiguous cmd\n");
> -					break;
>  				}
> +				break;
>  			}
>  		}
>  
> -- 
> 2.13.3
>
  
Olivier Matz Dec. 8, 2017, 1:51 p.m. UTC | #2
On Fri, Dec 08, 2017 at 01:27:26PM +0100, Adrien Mazarguil wrote:
> On Fri, Dec 08, 2017 at 03:02:44PM +0800, Xueming Li wrote:
> > Initialize binary result memory before parsing to avoid garbage in
> > parsing result.
> > 
> > Signed-off-by: Xueming Li <xuemingl@mellanox.com>
> 
> Since you chose to move the break statement, maybe the original commit
> mentioned in my previous message (9b3fbb051d2e "cmdline: fix parsing") can
> be reverted afterward? I think it makes tmp_result redundant.
> 
> Wenzhuo, as the author of that commit, can you confirm?
> 
> Olivier, no problem with breaking the loop immediately after the first
> successful match_inst() call instead of the last one? (I don't see why it
> would be an issue but I may have missed something)

Moving the break will change the behavior, it will never detect
ambiguous commands (i.e when several commands match the same input).
So I think we should not do it.

IMO, only the memset() is enough.

Regards,
Olivier
  
Xueming Li Dec. 8, 2017, 2:50 p.m. UTC | #3
> -----Original Message-----

> From: Olivier MATZ [mailto:olivier.matz@6wind.com]

> Sent: Friday, December 8, 2017 9:51 PM

> To: Adrien Mazarguil <adrien.mazarguil@6wind.com>

> Cc: Xueming(Steven) Li <xuemingl@mellanox.com>; Wenzhuo Lu

> <wenzhuo.lu@intel.com>; dev@dpdk.org

> Subject: Re: [dpdk-dev] [PATCH v1] lib/cmdline: init parse result memory

> 

> On Fri, Dec 08, 2017 at 01:27:26PM +0100, Adrien Mazarguil wrote:

> > On Fri, Dec 08, 2017 at 03:02:44PM +0800, Xueming Li wrote:

> > > Initialize binary result memory before parsing to avoid garbage in

> > > parsing result.

> > >

> > > Signed-off-by: Xueming Li <xuemingl@mellanox.com>

> >

> > Since you chose to move the break statement, maybe the original commit

> > mentioned in my previous message (9b3fbb051d2e "cmdline: fix parsing")

> > can be reverted afterward? I think it makes tmp_result redundant.

> >

> > Wenzhuo, as the author of that commit, can you confirm?

> >

> > Olivier, no problem with breaking the loop immediately after the first

> > successful match_inst() call instead of the last one? (I don't see why

> > it would be an issue but I may have missed something)

> 

> Moving the break will change the behavior, it will never detect ambiguous

> commands (i.e when several commands match the same input).

> So I think we should not do it.

> 

We don't have CLI performance issue all the time, but recently I'm using files 
to save hundreds of batch 'expect' test commands, execute with a 'load' command,
so performance of CLI processing becomes important.
CLI developer should make sure not defining a CLI that already exists and verify
the new CLI working as expected.

> IMO, only the memset() is enough.

> 

> Regards,

> Olivier
  

Patch

diff --git a/lib/librte_cmdline/cmdline_parse.c b/lib/librte_cmdline/cmdline_parse.c
index 3e12ee54f..4072103f2 100644
--- a/lib/librte_cmdline/cmdline_parse.c
+++ b/lib/librte_cmdline/cmdline_parse.c
@@ -168,6 +168,9 @@  match_inst(cmdline_parse_inst_t *inst, const char *buf,
 	int n = 0;
 	struct cmdline_token_hdr token_hdr;
 
+	if (resbuf != NULL)
+		memset(resbuf, 0, resbuf_size);
+
 	/* check if we match all tokens of inst */
 	while (!nb_match_token || i < nb_match_token) {
 		token_p = get_token(inst, i);
@@ -338,8 +341,8 @@  cmdline_parse(struct cmdline *cl, const char * buf)
 					err = CMDLINE_PARSE_AMBIGUOUS;
 					f=NULL;
 					debug_printf("Ambiguous cmd\n");
-					break;
 				}
+				break;
 			}
 		}