[PATCH 5/5] pi_stress: Ensure null-termination of JSON filename

John Kacur <[email protected]> Tue, 30 Jun 2026 12:45:02 -0400
Newsgroups org.kernel.vger.linux-rt-users
Message-ID <[email protected]>
The --json option used strncpy() with strnlen() to copy the filename,
but did not guarantee null-termination if the input string was exactly
MAX_PATH-1 characters long. When strncpy() copies exactly n characters,
it does not add a null terminator, potentially causing undefined
behavior when the string is later used.

Replace the strnlen() call with MAX_PATH-1 as the copy limit and
explicitly null-terminate the buffer to ensure the string is always
properly terminated regardless of input length.

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

diff --git a/src/pi_tests/pi_stress.c b/src/pi_tests/pi_stress.c
index 45ccac739646..500b10d60737 100644
--- a/src/pi_tests/pi_stress.c
+++ b/src/pi_tests/pi_stress.c
@@ -1238,7 +1238,8 @@ void process_command_line(int argc, char **argv)
 			pi_info("doing %d inversion per group\n", inversions);
 			break;
 		case OPT_JSON:
-			strncpy(jsonfile, optarg, strnlen(optarg, MAX_PATH-1));
+			strncpy(jsonfile, optarg, MAX_PATH-1);
+		jsonfile[MAX_PATH-1] = '\0';
 			break;
 		case OPT_MLOCKALL:
 		case 'm':
-- 
2.54.0