From e31f9bf324e72a313142705bbd93155a6038efb0 Mon Sep 17 00:00:00 2001 From: Anas Nashif Date: Tue, 17 Jun 2025 13:38:24 -0400 Subject: [PATCH] ztest: native_sim: fail instead of crashing on wrong arguments Instead of crashing fail with a message when -test arguments is of wrong format. Fixes zephyrproject-rtos/zephyr#91655 Signed-off-by: Anas Nashif --- subsys/testsuite/ztest/src/ztest_posix.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/subsys/testsuite/ztest/src/ztest_posix.c b/subsys/testsuite/ztest/src/ztest_posix.c index 744219538cd..c00eb083b70 100644 --- a/subsys/testsuite/ztest/src/ztest_posix.c +++ b/subsys/testsuite/ztest/src/ztest_posix.c @@ -11,6 +11,7 @@ #include #include #include "nsi_host_trampolines.h" +#include static const char *test_args; static bool list_tests; @@ -161,10 +162,18 @@ static bool z_ztest_testargs_contains(const char *suite_name, const char *test_n suite_arg = strtok_r(suite_test_pair, ":", &last_arg); test_arg = strtok_r(NULL, ":", &last_arg); + /* Validate format: must be test_suite::* or test_suite::test_case */ + if (!suite_arg || !test_arg || strtok_r(NULL, ":", &last_arg) != NULL) { + /* Invalid format, report error and exit */ + nsi_print_error_and_exit( + "Invalid test argument format '%s'. Expected a set of pairs like" + "'suite::test' or 'suite::*', got instead '%s'.\n", + test_args, suite_test_pair); + } + found = !strcmp(suite_arg, suite_name); if (test_name) { - found &= !strcmp(test_arg, "*") || - !strcmp(test_arg, test_name); + found &= (!strcmp(test_arg, "*") || !strcmp(test_arg, test_name)); } suite_test_pair = strtok_r(NULL, ",", &last_suite_test_pair);