[PATCH v2 02/39] argparse: check for range overflow in CPU lists

Bruce Richardson <[email protected]>
Newsgroups org.dpdk.dev
Message-ID <[email protected]>
The rte_cpuset_t type cannot handle core ids greater than CPU_SETSIZE
Add an explicit check for out of range values when processes a corelist.

Fixes: d78103fb9488 ("argparse: support core lists")
Cc: [email protected]

Signed-off-by: Bruce Richardson <[email protected]>
---
 app/test/test_argparse.c    | 10 ++++++++++
 lib/argparse/rte_argparse.c |  5 +++++
 2 files changed, 15 insertions(+)

diff --git a/app/test/test_argparse.c b/app/test/test_argparse.c
index fdc6dd113c..318f8df9fb 100644
--- a/app/test/test_argparse.c
+++ b/app/test/test_argparse.c
@@ -834,9 +834,13 @@ test_argparse_parse_type_corelist(void)
 	char *corelist_invalid_special_chars = test_strdup("1,2@3");
 	char *corelist_invalid_comma_only = test_strdup(",");
 	char *corelist_invalid_out_of_range = test_strdup("70000");
+	char corelist_invalid_cpu_set[32];
 	rte_cpuset_t val_cpuset;
 	int ret;
 
+	snprintf(corelist_invalid_cpu_set, sizeof(corelist_invalid_cpu_set),
+		"0-%d", CPU_SETSIZE);
+
 	/* test valid single core */
 	CPU_ZERO(&val_cpuset);
 	ret = rte_argparse_parse_type(corelist_valid_single,
@@ -966,6 +970,12 @@ test_argparse_parse_type_corelist(void)
 			RTE_ARGPARSE_VALUE_TYPE_CORELIST, &val_cpuset);
 	TEST_ASSERT(ret != 0, "Argparse parse type for corelist (out of range) should have failed!");
 
+	/* test invalid corelist that exceeds the destination CPU set */
+	CPU_ZERO(&val_cpuset);
+	ret = rte_argparse_parse_type(corelist_invalid_cpu_set,
+			RTE_ARGPARSE_VALUE_TYPE_CORELIST, &val_cpuset);
+	TEST_ASSERT(ret != 0, "Argparse parse type for corelist outside CPU set should have failed!");
+
 	return 0;
 }
 
diff --git a/lib/argparse/rte_argparse.c b/lib/argparse/rte_argparse.c
index 88c3e7740c..158c413bdc 100644
--- a/lib/argparse/rte_argparse.c
+++ b/lib/argparse/rte_argparse.c
@@ -623,6 +623,11 @@ parse_arg_corelist(const struct rte_argparse_arg *arg, const char *value)
 				/* range from low to high */
 				max = idx;
 			}
+			if (max >= CPU_SETSIZE) {
+				ARGPARSE_LOG(ERR, "argument %s contains a core outside the CPU set range!",
+					arg->name_long);
+				return -EINVAL;
+			}
 
 			for (; min <= max; min++)
 				CPU_SET(min, cpuset);
-- 
2.53.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.