kunit: Fix possible memory leak in kunit_filter_suites()
authorJinjie Ruan <ruanjinjie@huawei.com>
Wed, 27 Sep 2023 09:03:49 +0000 (17:03 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 20 Nov 2023 10:59:15 +0000 (11:59 +0100)
commit71edfc95113890ef872544c947b1f113519da8f4
treeaee42cd6abdc285fc142c856d0cf452c654447f3
parent421058c75b8339d5d898a8cde36e058cdf85393e
kunit: Fix possible memory leak in kunit_filter_suites()

[ Upstream commit 24de14c98b37ea40a7e493dfd0d93b400b6efbca ]

If the outer layer for loop is iterated more than once and it fails not
in the first iteration, the filtered_suite and filtered_suite->test_cases
allocated in the last kunit_filter_attr_tests() in last inner for loop
is leaked.

So add a new free_filtered_suite err label and free the filtered_suite
and filtered_suite->test_cases so far. And change kmalloc_array of copy
to kcalloc to Clear the copy to make the kfree safe.

Fixes: 529534e8cba3 ("kunit: Add ability to filter attributes")
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
Reviewed-by: Rae Moar <rmoar@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
lib/kunit/executor.c