Skip to content

Commit 37dbb4c

Browse files
sulixshuahkh
authored andcommitted
kunit: Don't crash if no parameters are generated
It's possible that a parameterised test could end up with zero parameters. At the moment, the test function will nevertheless be called with NULL as the parameter. Instead, don't try to run the test code, and just mark the test as SKIPped. Reported-by: Daniel Latypov <[email protected]> Signed-off-by: David Gow <[email protected]> Reviewed-by: Daniel Latypov <[email protected]> Reviewed-by: Brendan Higgins <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent e56e482 commit 37dbb4c

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

lib/kunit/test.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -504,16 +504,18 @@ int kunit_run_tests(struct kunit_suite *suite)
504504
struct kunit_result_stats param_stats = { 0 };
505505
test_case->status = KUNIT_SKIPPED;
506506

507-
if (test_case->generate_params) {
507+
if (!test_case->generate_params) {
508+
/* Non-parameterised test. */
509+
kunit_run_case_catch_errors(suite, test_case, &test);
510+
kunit_update_stats(&param_stats, test.status);
511+
} else {
508512
/* Get initial param. */
509513
param_desc[0] = '\0';
510514
test.param_value = test_case->generate_params(NULL, param_desc);
511-
}
512515

513-
do {
514-
kunit_run_case_catch_errors(suite, test_case, &test);
516+
while (test.param_value) {
517+
kunit_run_case_catch_errors(suite, test_case, &test);
515518

516-
if (test_case->generate_params) {
517519
if (param_desc[0] == '\0') {
518520
snprintf(param_desc, sizeof(param_desc),
519521
"param-%d", test.param_index);
@@ -530,11 +532,11 @@ int kunit_run_tests(struct kunit_suite *suite)
530532
param_desc[0] = '\0';
531533
test.param_value = test_case->generate_params(test.param_value, param_desc);
532534
test.param_index++;
533-
}
534535

535-
kunit_update_stats(&param_stats, test.status);
536+
kunit_update_stats(&param_stats, test.status);
537+
}
538+
}
536539

537-
} while (test.param_value);
538540

539541
kunit_print_test_stats(&test, param_stats);
540542

0 commit comments

Comments
 (0)