DPDK-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] eal: check for invalid memory parameters
@ 2026-06-04 16:12 Stephen Hemminger
  2026-06-08  7:10 ` Konstantin Ananyev
  0 siblings, 1 reply; 3+ messages in thread
From: Stephen Hemminger @ 2026-06-04 16:12 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

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;
 	}
 	if (args.no_huge) {
 		int_cfg->no_hugetlbfs = 1;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* RE: [PATCH] eal: check for invalid memory parameters
  2026-06-04 16:12 Stephen Hemminger
@ 2026-06-08  7:10 ` Konstantin Ananyev
  0 siblings, 0 replies; 3+ messages in thread
From: Konstantin Ananyev @ 2026-06-08  7:10 UTC (permalink / raw)
  To: Stephen Hemminger, dev@dpdk.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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] eal: check for invalid memory parameters
@ 2026-07-23 17:45 Stephen Hemminger
  0 siblings, 0 replies; 3+ messages in thread
From: Stephen Hemminger @ 2026-07-23 17:45 UTC (permalink / raw)
  To: dev; +Cc: Stephen Hemminger

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 strtoul().

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 42cdef632f..bda60e6394 100644
--- a/lib/eal/common/eal_common_options.c
+++ b/lib/eal/common/eal_common_options.c
@@ -2067,6 +2067,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 n;
+
+	while (isspace((unsigned char)*str))
+		str++;
+
+	if (*str == '-')
+		return -1;
+
+	errno = 0;
+	n = strtoul(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)
@@ -2210,23 +2233,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;
 	}
 	if (args.no_huge) {
 		int_cfg->no_hugetlbfs = 1;
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-07-23 17:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-23 17:45 [PATCH] eal: check for invalid memory parameters Stephen Hemminger
  -- strict thread matches above, loose matches on Subject: below --
2026-06-04 16:12 Stephen Hemminger
2026-06-08  7:10 ` Konstantin Ananyev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox