[PATCH 2/5] pi_stress: Validate numeric command-line arguments

John Kacur <[email protected]> Tue, 30 Jun 2026 12:44:59 -0400
Newsgroups org.kernel.vger.linux-rt-users
Message-ID <[email protected]>
The parse_unsigned() and parse_signed() functions were using strtoul()
and strtol() but not checking if the entire string was consumed. This
allowed invalid input like "123abc" to be silently accepted as "123".

Additionally, the -g (groups) and -i (inversions) options were calling
strtol() directly instead of using the parse_signed() helper function,
bypassing any validation entirely.

Add validation to parse_unsigned() and parse_signed() to ensure the
entire input string is a valid number by checking that the end pointer
points to a null terminator. Update -g and -i options to use
parse_signed() for consistent error handling.

Assisted-by: Claude:claude-sonnet-4-5
Signed-off-by: John Kacur <[email protected]>
---
 src/pi_tests/pi_stress.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/pi_tests/pi_stress.c b/src/pi_tests/pi_stress.c
index b7f7515fa9b1..c8c895a71e44 100644
--- a/src/pi_tests/pi_stress.c
+++ b/src/pi_tests/pi_stress.c
@@ -1092,6 +1092,11 @@ unsigned long parse_unsigned(const char *str)
 		exit(EXIT_FAILURE);
 	}
 
+	if (*p != '\0') {
+		pi_error("invalid number format: %s\n", str);
+		exit(EXIT_FAILURE);
+	}
+
 	return n;
 }
 
@@ -1109,6 +1114,11 @@ long parse_signed(const char *str)
 		exit(EXIT_FAILURE);
 	}
 
+	if (*p != '\0') {
+		pi_error("invalid number format: %s\n", str);
+		exit(EXIT_FAILURE);
+	}
+
 	return n;
 }
 
@@ -1211,7 +1221,7 @@ void process_command_line(int argc, char **argv)
 			break;
 		case OPT_GROUPS:
 		case 'g':
-			ngroups = strtol(optarg, NULL, 10);
+			ngroups = parse_signed(optarg);
 			if (ngroups > num_processors) {
 				pi_error("the number of groups cannot exceed the number of online processors (%ld)\n", num_processors);
 				exit(-1);
@@ -1224,7 +1234,7 @@ void process_command_line(int argc, char **argv)
 			break;
 		case OPT_INVERSIONS:
 		case 'i':
-			inversions = strtol(optarg, NULL, 10);
+			inversions = parse_signed(optarg);
 			pi_info("doing %d inversion per group\n", inversions);
 			break;
 		case OPT_JSON:
-- 
2.54.0