From: Konstantin Ananyev <konstantin.ananyev@huawei.com>
To: Stephen Hemminger <stephen@networkplumber.org>,
"dev@dpdk.org" <dev@dpdk.org>
Subject: RE: [PATCH] eal: check for invalid memory parameters
Date: Mon, 8 Jun 2026 07:10:52 +0000 [thread overview]
Message-ID: <0ab8c5ebbe024f1aa29bd4a5fb77f81f@huawei.com> (raw)
In-Reply-To: <20260604161224.1090419-1-stephen@networkplumber.org>
> The code to parse arguments like memory size, channels and rank
> was using atoi() which has no check for garbage after the number.
> Switch to using a helper that uses strtoull().
>
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> ---
> lib/eal/common/eal_common_options.c | 44 +++++++++++++++++++++++++----
> 1 file changed, 39 insertions(+), 5 deletions(-)
>
> diff --git a/lib/eal/common/eal_common_options.c
> b/lib/eal/common/eal_common_options.c
> index 1049838d73..49151c0a16 100644
> --- a/lib/eal/common/eal_common_options.c
> +++ b/lib/eal/common/eal_common_options.c
> @@ -2062,6 +2062,29 @@ eal_parse_huge_worker_stack(const char *arg)
> return 0;
> }
>
> +static int
> +eal_parse_num(const char *str, unsigned int *val)
> +{
> + char *endptr;
> + unsigned long long n;
> +
> + while (isspace((unsigned char)*str))
> + str++;
> +
> + if (*str == '-')
> + return -1;
> +
> + errno = 0;
> + n = strtoull(str, &endptr, 10);
> +
> + /* Error if string is empty or has trailing characters */
> + if (*str == '\0' || *endptr != '\0' || errno != 0 || n > UINT_MAX)
> + return -1;
> +
> + *val = n;
> + return 0;
> +}
> +
> /* Parse the arguments given in the command line of the application */
> int
> eal_parse_args(void)
> @@ -2205,23 +2228,34 @@ eal_parse_args(void)
>
> /* memory options */
> if (args.memory_size != NULL) {
> - int_cfg->memory = atoi(args.memory_size);
> + unsigned int mb;
> + if (eal_parse_num(args.memory_size, &mb) < 0) {
> + EAL_LOG(ERR, "invalid memory size parameter");
> + return -1;
> + }
> +
> + int_cfg->memory = mb;
> int_cfg->memory *= 1024ULL;
> int_cfg->memory *= 1024ULL;
> }
> if (args.memory_channels != NULL) {
> - int_cfg->force_nchannel = atoi(args.memory_channels);
> - if (int_cfg->force_nchannel == 0) {
> + unsigned int n;
> + if (eal_parse_num(args.memory_channels, &n) < 0 ||
> + n == 0 || n > 32) {
> EAL_LOG(ERR, "invalid memory channel parameter");
> return -1;
> }
> + int_cfg->force_nchannel = n;
> }
> if (args.memory_ranks != NULL) {
> - int_cfg->force_nrank = atoi(args.memory_ranks);
> - if (int_cfg->force_nrank == 0 || int_cfg->force_nrank > 16) {
> + unsigned int n;
> +
> + if (eal_parse_num(args.memory_ranks, &n) < 0 ||
> + n == 0 || n > 16) {
> EAL_LOG(ERR, "invalid memory rank parameter");
> return -1;
> }
> + int_cfg->force_nrank = n;
It is probably worth to replace hardcoded constants (16/32) with some macros.
Acked-by: Konstantin Ananyev <konstantin.ananyev@huawei.com>
> }
> if (args.no_huge) {
> int_cfg->no_hugetlbfs = 1;
> --
> 2.53.0
prev parent reply other threads:[~2026-06-08 7:10 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-04 16:12 [PATCH] eal: check for invalid memory parameters Stephen Hemminger
2026-06-08 7:10 ` Konstantin Ananyev [this message]
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=0ab8c5ebbe024f1aa29bd4a5fb77f81f@huawei.com \
--to=konstantin.ananyev@huawei.com \
--cc=dev@dpdk.org \
--cc=stephen@networkplumber.org \
/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