From patchwork Sat Dec 9 15:39:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xueming Li X-Patchwork-Id: 32055 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 225F82FDD; Sat, 9 Dec 2017 16:39:43 +0100 (CET) Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by dpdk.org (Postfix) with ESMTP id 2DF962030 for ; Sat, 9 Dec 2017 16:39:42 +0100 (CET) Received: from Internal Mail-Server by MTLPINE1 (envelope-from xuemingl@mellanox.com) with ESMTPS (AES256-SHA encrypted); 9 Dec 2017 17:39:36 +0200 Received: from dev-r630-06.mtbc.labs.mlnx (dev-r630-06.mtbc.labs.mlnx [10.12.205.180]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id vB9FdZc6029794; Sat, 9 Dec 2017 17:39:36 +0200 Received: from dev-r630-06.mtbc.labs.mlnx (localhost [127.0.0.1]) by dev-r630-06.mtbc.labs.mlnx (8.14.7/8.14.7) with ESMTP id vB9FdYk0020004; Sat, 9 Dec 2017 23:39:34 +0800 Received: (from xuemingl@localhost) by dev-r630-06.mtbc.labs.mlnx (8.14.7/8.14.7/Submit) id vB9FdUMs020003; Sat, 9 Dec 2017 23:39:30 +0800 From: Xueming Li To: Olivier MATZ , Adrien Mazarguil Cc: Xueming Li , dev@dpdk.org Date: Sat, 9 Dec 2017 23:39:23 +0800 Message-Id: <20171209153923.19958-1-xuemingl@mellanox.com> X-Mailer: git-send-email 2.13.3 In-Reply-To: <20171115155402.9967-1-xuemingl@mellanox.com> References: <20171115155402.9967-1-xuemingl@mellanox.com> Subject: [dpdk-dev] [PATCH v2] lib/cmdline: init CLI parsing memory X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Initialize result memory every time before parsing. Also save successfully parsed result before further ambiguous command detection to avoid result being tainted by later parsing. Signed-off-by: Xueming Li --- lib/librte_cmdline/cmdline_parse.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/librte_cmdline/cmdline_parse.c b/lib/librte_cmdline/cmdline_parse.c index 3e12ee54f..45117789a 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); @@ -251,7 +254,7 @@ cmdline_parse(struct cmdline *cl, const char * buf) union { char buf[CMDLINE_PARSE_RESULT_BUFSIZE]; long double align; /* strong alignment constraint for buf */ - } result, tmp_result; + } result, result_ok; void (*f)(void *, struct cmdline *, void *) = NULL; void *data = NULL; int comment = 0; @@ -312,16 +315,13 @@ cmdline_parse(struct cmdline *cl, const char * buf) debug_printf("INST %d\n", inst_num); /* fully parsed */ - tok = match_inst(inst, buf, 0, tmp_result.buf, - sizeof(tmp_result.buf)); + tok = match_inst(inst, buf, 0, result.buf, sizeof(result.buf)); if (tok > 0) /* we matched at least one token */ err = CMDLINE_PARSE_BAD_ARGS; else if (!tok) { debug_printf("INST fully parsed\n"); - memcpy(&result, &tmp_result, - sizeof(result)); /* skip spaces */ while (isblank2(*curbuf)) { curbuf++; @@ -332,6 +332,7 @@ cmdline_parse(struct cmdline *cl, const char * buf) if (!f) { memcpy(&f, &inst->f, sizeof(f)); memcpy(&data, &inst->data, sizeof(data)); + result_ok = result; } else { /* more than 1 inst matches */ @@ -349,6 +350,7 @@ cmdline_parse(struct cmdline *cl, const char * buf) /* call func */ if (f) { + result = result_ok; f(result.buf, cl, data); }