Skip to content

Commit 1c9fd08

Browse files
rmr167shuahkh
authored andcommitted
kunit: fix uninitialized variables bug in attributes filtering
Fix smatch warnings regarding uninitialized variables in the filtering patch of the new KUnit Attributes feature. Fixes: 529534e ("kunit: Add ability to filter attributes") Reported-by: kernel test robot <lkp@intel.com> Reported-by: Dan Carpenter <dan.carpenter@linaro.org> Closes: https://lore.kernel.org/r/202307270610.s0w4NKEn-lkp@intel.com/ Signed-off-by: Rae Moar <rmoar@google.com> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent abbf738 commit 1c9fd08

File tree

3 files changed

+29
-31
lines changed

3 files changed

+29
-31
lines changed

lib/kunit/attributes.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static int int_filter(long val, const char *op, int input, int *err)
102102
static int attr_enum_filter(void *attr, const char *input, int *err,
103103
const char * const str_list[], int max)
104104
{
105-
int i, j, input_int;
105+
int i, j, input_int = -1;
106106
long test_val = (long)attr;
107107
const char *input_val = NULL;
108108

@@ -124,7 +124,7 @@ static int attr_enum_filter(void *attr, const char *input, int *err,
124124
input_int = j;
125125
}
126126

127-
if (!input_int) {
127+
if (input_int < 0) {
128128
*err = -EINVAL;
129129
pr_err("kunit executor: invalid filter input: %s\n", input);
130130
return false;
@@ -186,8 +186,10 @@ static void *attr_module_get(void *test_or_suite, bool is_test)
186186
// Suites get their module attribute from their first test_case
187187
if (test)
188188
return ((void *) test->module_name);
189-
else
189+
else if (kunit_suite_num_test_cases(suite) > 0)
190190
return ((void *) suite->test_cases[0].module_name);
191+
else
192+
return (void *) "";
191193
}
192194

193195
/* List of all Test Attributes */
@@ -221,7 +223,7 @@ const char *kunit_attr_filter_name(struct kunit_attr_filter filter)
221223
void kunit_print_attr(void *test_or_suite, bool is_test, unsigned int test_level)
222224
{
223225
int i;
224-
bool to_free;
226+
bool to_free = false;
225227
void *attr;
226228
const char *attr_name, *attr_str;
227229
struct kunit_suite *suite = is_test ? NULL : test_or_suite;
@@ -255,7 +257,7 @@ void kunit_print_attr(void *test_or_suite, bool is_test, unsigned int test_level
255257

256258
int kunit_get_filter_count(char *input)
257259
{
258-
int i, comma_index, count = 0;
260+
int i, comma_index = 0, count = 0;
259261

260262
for (i = 0; input[i]; i++) {
261263
if (input[i] == ',') {
@@ -272,7 +274,7 @@ int kunit_get_filter_count(char *input)
272274
struct kunit_attr_filter kunit_next_attr_filter(char **filters, int *err)
273275
{
274276
struct kunit_attr_filter filter = {};
275-
int i, j, comma_index, new_start_index;
277+
int i, j, comma_index = 0, new_start_index = 0;
276278
int op_index = -1, attr_index = -1;
277279
char op;
278280
char *input = *filters;
@@ -316,7 +318,7 @@ struct kunit_attr_filter kunit_next_attr_filter(char **filters, int *err)
316318
filter.attr = &kunit_attr_list[attr_index];
317319
}
318320

319-
if (comma_index) {
321+
if (comma_index > 0) {
320322
input[comma_index] = '\0';
321323
filter.input = input + op_index;
322324
input = input + new_start_index;
@@ -356,31 +358,22 @@ struct kunit_suite *kunit_filter_attr_tests(const struct kunit_suite *const suit
356358

357359
/* Save filtering result on default value */
358360
default_result = filter.attr->filter(filter.attr->attr_default, filter.input, err);
359-
if (*err) {
360-
kfree(copy);
361-
kfree(filtered);
362-
return NULL;
363-
}
361+
if (*err)
362+
goto err;
364363

365364
/* Save suite attribute value and filtering result on that value */
366365
suite_val = filter.attr->get_attr((void *)suite, false);
367366
suite_result = filter.attr->filter(suite_val, filter.input, err);
368-
if (*err) {
369-
kfree(copy);
370-
kfree(filtered);
371-
return NULL;
372-
}
367+
if (*err)
368+
goto err;
373369

374370
/* For each test case, save test case if passes filtering. */
375371
kunit_suite_for_each_test_case(suite, test_case) {
376372
test_val = filter.attr->get_attr((void *) test_case, true);
377373
test_result = filter.attr->filter(filter.attr->get_attr(test_case, true),
378374
filter.input, err);
379-
if (*err) {
380-
kfree(copy);
381-
kfree(filtered);
382-
return NULL;
383-
}
375+
if (*err)
376+
goto err;
384377

385378
/*
386379
* If attribute value of test case is set, filter on that value.
@@ -406,7 +399,8 @@ struct kunit_suite *kunit_filter_attr_tests(const struct kunit_suite *const suit
406399
}
407400
}
408401

409-
if (n == 0) {
402+
err:
403+
if (n == 0 || *err) {
410404
kfree(copy);
411405
kfree(filtered);
412406
return NULL;

lib/kunit/executor.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,27 +127,30 @@ static struct suite_set kunit_filter_suites(const struct suite_set *suite_set,
127127
{
128128
int i, j, k;
129129
int filter_count = 0;
130-
struct kunit_suite **copy, *filtered_suite, *new_filtered_suite;
131-
struct suite_set filtered;
130+
struct kunit_suite **copy, **copy_start, *filtered_suite, *new_filtered_suite;
131+
struct suite_set filtered = {NULL, NULL};
132132
struct kunit_glob_filter parsed_glob;
133-
struct kunit_attr_filter *parsed_filters;
133+
struct kunit_attr_filter *parsed_filters = NULL;
134134

135135
const size_t max = suite_set->end - suite_set->start;
136136

137137
copy = kmalloc_array(max, sizeof(*filtered.start), GFP_KERNEL);
138-
filtered.start = copy;
139138
if (!copy) { /* won't be able to run anything, return an empty set */
140-
filtered.end = copy;
141139
return filtered;
142140
}
141+
copy_start = copy;
143142

144143
if (filter_glob)
145144
kunit_parse_glob_filter(&parsed_glob, filter_glob);
146145

147146
/* Parse attribute filters */
148147
if (filters) {
149148
filter_count = kunit_get_filter_count(filters);
150-
parsed_filters = kcalloc(filter_count + 1, sizeof(*parsed_filters), GFP_KERNEL);
149+
parsed_filters = kcalloc(filter_count, sizeof(*parsed_filters), GFP_KERNEL);
150+
if (!parsed_filters) {
151+
kfree(copy);
152+
return filtered;
153+
}
151154
for (j = 0; j < filter_count; j++)
152155
parsed_filters[j] = kunit_next_attr_filter(&filters, err);
153156
if (*err)
@@ -166,7 +169,7 @@ static struct suite_set kunit_filter_suites(const struct suite_set *suite_set,
166169
goto err;
167170
}
168171
}
169-
if (filter_count) {
172+
if (filter_count > 0 && parsed_filters != NULL) {
170173
for (k = 0; k < filter_count; k++) {
171174
new_filtered_suite = kunit_filter_attr_tests(filtered_suite,
172175
parsed_filters[k], filter_action, err);
@@ -195,6 +198,7 @@ static struct suite_set kunit_filter_suites(const struct suite_set *suite_set,
195198

196199
*copy++ = filtered_suite;
197200
}
201+
filtered.start = copy_start;
198202
filtered.end = copy;
199203

200204
err:

lib/kunit/executor_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static void parse_filter_attr_test(struct kunit *test)
119119
filter_count = kunit_get_filter_count(filters);
120120
KUNIT_EXPECT_EQ(test, filter_count, 2);
121121

122-
parsed_filters = kunit_kcalloc(test, filter_count + 1, sizeof(*parsed_filters),
122+
parsed_filters = kunit_kcalloc(test, filter_count, sizeof(*parsed_filters),
123123
GFP_KERNEL);
124124
for (j = 0; j < filter_count; j++) {
125125
parsed_filters[j] = kunit_next_attr_filter(&filters, &err);

0 commit comments

Comments
 (0)