[PATCH 1/3] kunit: fix filter attribute list parsing with a trailing comma

From: Miao Tianzhong

Date: Thu Dec 03 2026 - 21:57:06 EST


kunit.filter_attr= takes a comma separated list of attribute filters.
When the list ends with a comma (e.g. "kunit.filter_attr=speed=slow,")
kunit_get_filter_count() counts the empty trailing segment as an extra
filter, and kunit_next_attr_filter() then leaves the cursor pointing at
the beginning of the string instead of terminating the list, so the same
filter is parsed and applied twice.

Only count segments that contain a filter, and terminate the list in
kunit_next_attr_filter() when nothing but whitespace follows the last
comma.

Fixes: 529534e8cba3 ("kunit: Add ability to filter attributes")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Miao Tianzhong <3514767322@xxxxxx>
---
lib/kunit/attributes.c | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/lib/kunit/attributes.c b/lib/kunit/attributes.c
index 6d7a53af94a9..9d368a2844d8 100644
--- a/lib/kunit/attributes.c
+++ b/lib/kunit/attributes.c
@@ -319,17 +319,21 @@ void kunit_print_attr(void *test_or_suite, bool is_test, unsigned int test_level

int kunit_get_filter_count(char *input)
{
- int i, comma_index = 0, count = 0;
+ int i, count = 0;
+ bool has_content = false;

for (i = 0; input[i]; i++) {
if (input[i] == ',') {
- if ((i - comma_index) > 1)
+ if (has_content)
count++;
- comma_index = i;
+ has_content = false;
+ } else if (input[i] != ' ') {
+ has_content = true;
}
}
- if ((i - comma_index) > 0)
+ if (has_content)
count++;
+
return count;
}

@@ -383,7 +387,14 @@ struct kunit_attr_filter kunit_next_attr_filter(char **filters, int *err)
if (comma_index > 0) {
input[comma_index] = '\0';
filter.input = input + op_index;
- input = input + new_start_index;
+ /*
+ * Nothing but whitespace after the last comma: the filter
+ * list ends here.
+ */
+ if (new_start_index > comma_index)
+ input = input + new_start_index;
+ else
+ input = NULL;
} else {
filter.input = input + op_index;
input = NULL;
--
2.56.1