From: Adrien Mazarguil Date: Mon, 10 Jul 2017 12:09:36 +0000 (+0200) Subject: app/testpmd: fix token matching in flow command X-Git-Tag: spdx-start~2380 X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;h=a00cbb4586bdff936c81a9179a93b869c52e9b0c;p=dpdk.git app/testpmd: fix token matching in flow command While matching user input against a token name or any other fixed string, comparison stops at the end of user input if shorter (e.g. "foo" matches token name "foobar"). Although the unintended consequence of this behavior allows users to abbreviate command names and various parameters yet generate valid commands, the parser was not designed to support this and does not prevent ambiguous tokens. For instance, entering "i" for a pattern item matches "ipv4", "ipv6" and "icmp" then defaults to one of them in an unspecified manner. Prevent this behavior by taking the length of fixed strings into account. Fixes: 19c90af6285c ("app/testpmd: add flow command") Fixes: 5ac3502ed1be ("app/testpmd: add flow query command") Fixes: abc3d81aca1b ("app/testpmd: add item raw to flow command") Fixes: 05d34c6e9d2c ("app/testpmd: add queue actions to flow command") Cc: stable@dpdk.org Signed-off-by: Adrien Mazarguil --- diff --git a/app/test-pmd/cmdline_flow.c b/app/test-pmd/cmdline_flow.c index 9533df1feb..a17a004329 100644 --- a/app/test-pmd/cmdline_flow.c +++ b/app/test-pmd/cmdline_flow.c @@ -1624,6 +1624,19 @@ arg_entry_bf_fill(void *dst, uintmax_t val, const struct arg *arg) return len; } +/** Compare a string with a partial one of a given length. */ +static int +strcmp_partial(const char *full, const char *partial, size_t partial_len) +{ + int r = strncmp(full, partial, partial_len); + + if (r) + return r; + if (strlen(full) <= partial_len) + return 0; + return full[partial_len]; +} + /** * Parse a prefix length and generate a bit-mask. * @@ -1706,7 +1719,7 @@ parse_default(struct context *ctx, const struct token *token, (void)ctx; (void)buf; (void)size; - if (strncmp(str, token->name, len)) + if (strcmp_partial(token->name, str, len)) return -1; return len; } @@ -1949,7 +1962,7 @@ parse_vc_action_rss_queue(struct context *ctx, const struct token *token, if (ctx->curr != ACTION_RSS_QUEUE) return -1; i = ctx->objdata >> 16; - if (!strncmp(str, "end", len)) { + if (!strcmp_partial("end", str, len)) { ctx->objdata &= 0xffff; return len; } @@ -2084,7 +2097,7 @@ parse_action(struct context *ctx, const struct token *token, const struct parse_action_priv *priv; token = &token_list[next_action[i]]; - if (strncmp(token->name, str, len)) + if (strcmp_partial(token->name, str, len)) continue; priv = token->priv; if (!priv) @@ -2451,7 +2464,7 @@ parse_boolean(struct context *ctx, const struct token *token, if (!arg) return -1; for (i = 0; boolean_name[i]; ++i) - if (!strncmp(str, boolean_name[i], len)) + if (!strcmp_partial(boolean_name[i], str, len)) break; /* Process token as integer. */ if (boolean_name[i])