From: Olivier Matz <olivier.matz@6wind.com>
To: Piotr Azarewicz <piotrx.t.azarewicz@intel.com>
Cc: dev@dpdk.org
Subject: Re: [dpdk-dev] [PATCH v1 1/1] cmdline: add any multi string mode to token string
Date: Mon, 4 Apr 2016 10:00:46 +0200 [thread overview]
Message-ID: <57021F2E.8070306@6wind.com> (raw)
In-Reply-To: <1459510581-31392-1-git-send-email-piotrx.t.azarewicz@intel.com>
Hi Piotr,
This is globally ok for me. Please see a comment below.
On 04/01/2016 01:36 PM, Piotr Azarewicz wrote:
> @@ -162,12 +174,15 @@ cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
> }
>
> if (res) {
> - /* we are sure that token_len is < STR_TOKEN_SIZE-1 */
> - snprintf(res, STR_TOKEN_SIZE, "%s", buf);
> - *((char *)res + token_len) = 0;
> + if ((sd->str != NULL) && (strcmp(sd->str, TOKEN_STRING_MULTI) == 0))
> + snprintf(res, token_len + 1, "%s", buf);
> + else {
> + /* we are sure that token_len is < STR_TOKEN_SIZE-1 */
> + snprintf(res, STR_TOKEN_SIZE, "%s", buf);
> + *((char *)res + token_len) = 0;
> + }
> }
>
Using token_len + 1 as the buffer size in the snprintf looks a
bit dangerous, as it won't protect from overflows.
See the following example:
struct cmd_foo_result {
cmdline_fixed_string_t args;
cmdline_fixed_string_t foo;
};
static void
cmd_foo_parsed(void *parsed_result,
__rte_unused struct cmdline *cl,
__rte_unused void *data)
{
struct cmd_foo_result *res = parsed_result;
printf("foo=%s, args=%s\n", res->foo, res->args);
}
cmdline_parse_token_string_t cmd_foo_foo =
TOKEN_STRING_INITIALIZER(struct cmd_foo_result, foo,
"foo");
cmdline_parse_token_string_t cmd_foo_args =
TOKEN_STRING_INITIALIZER(struct cmd_foo_result, args,
TOKEN_STRING_MULTI);
cmdline_parse_inst_t cmd_foo = {
.f = cmd_foo_parsed, /* function to call */
.data = NULL, /* 2nd arg of func */
.help_str = "test",
.tokens = { /* token list, NULL terminated */
(void *)&cmd_foo_foo,
(void *)&cmd_foo_args,
NULL,
},
};
The result will be:
# ok
RTE>>foo xxx
foo=foo, args=xxx
# not ok, args overflows in foo
RTE>>foo
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
foo=xxxxxxxxxxxxxxxxxxxxxxx,
args=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
That's why snprintf() should still use STR_TOKEN_SIZE.
Regards,
Olivier
next prev parent reply other threads:[~2016-04-04 8:00 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-04-01 11:36 Piotr Azarewicz
2016-04-04 8:00 ` Olivier Matz [this message]
2016-04-04 14:11 ` Azarewicz, PiotrX T
2016-04-04 15:57 ` Olivier Matz
2016-04-05 6:58 ` Azarewicz, PiotrX T
2016-04-05 8:47 ` [dpdk-dev] [PATCH v2 " Piotr Azarewicz
2016-04-05 11:21 ` Olivier Matz
2016-04-05 15:39 ` Thomas Monjalon
2016-04-15 14:41 ` Wiles, Keith
2016-04-28 14:52 ` Olivier MATZ
2016-04-29 7:14 ` Azarewicz, PiotrX T
2016-04-29 14:29 ` [dpdk-dev] [PATCH v3 " Piotr Azarewicz
2016-05-02 13:32 ` Thomas Monjalon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=57021F2E.8070306@6wind.com \
--to=olivier.matz@6wind.com \
--cc=dev@dpdk.org \
--cc=piotrx.t.azarewicz@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).