Skip to content

Commit

Permalink
kunit: Don't crash if no parameters are generated
Browse files Browse the repository at this point in the history
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 <dlatypov@google.com>
Signed-off-by: David Gow <davidgow@google.com>
Reviewed-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
  • Loading branch information
David Gow authored and Shuah Khan committed Dec 13, 2021
1 parent e56e482 commit 37dbb4c
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/kunit/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -504,16 +504,18 @@ int kunit_run_tests(struct kunit_suite *suite)
struct kunit_result_stats param_stats = { 0 };
test_case->status = KUNIT_SKIPPED;

if (test_case->generate_params) {
if (!test_case->generate_params) {
/* Non-parameterised test. */
kunit_run_case_catch_errors(suite, test_case, &test);
kunit_update_stats(&param_stats, test.status);
} else {
/* Get initial param. */
param_desc[0] = '\0';
test.param_value = test_case->generate_params(NULL, param_desc);
}

do {
kunit_run_case_catch_errors(suite, test_case, &test);
while (test.param_value) {
kunit_run_case_catch_errors(suite, test_case, &test);

if (test_case->generate_params) {
if (param_desc[0] == '\0') {
snprintf(param_desc, sizeof(param_desc),
"param-%d", test.param_index);
Expand All @@ -530,11 +532,11 @@ int kunit_run_tests(struct kunit_suite *suite)
param_desc[0] = '\0';
test.param_value = test_case->generate_params(test.param_value, param_desc);
test.param_index++;
}

kunit_update_stats(&param_stats, test.status);
kunit_update_stats(&param_stats, test.status);
}
}

} while (test.param_value);

kunit_print_test_stats(&test, param_stats);

Expand Down

0 comments on commit 37dbb4c

Please sign in to comment.