kunit: executor: Fix a memory leak on failure in kunit_filter_tests
PATCH v2 1/1 kunit: executor: Fix a memory leak on failure in kunit_filter_tests
It's possible that memory allocation for 'filtered' will fail, but for the copy of the suite to succeed. In this case, the copy could be leaked.
Properly free 'copy' in the error case for the allocation of 'filtered' failing.
Note that there may also have been a similar issue in kunit_filter_subsuites, before it was removed in "kunit: flatten kunit_suite*** to kunit_suite** in .kunit_test_suites".
This was reported by clang-analyzer via the kernel test robot, here:
https://lore.kernel.org/all/[email protected]/
And by smatch via Dan Carpenter and the kernel test robot:
https://lore.kernel.org/all/[email protected]/
---
Thanks everyone! No actual code changes in v2, just fixes to the description.
Changes since v1:
https://lore.kernel.org/all/[email protected]/
- Fix a mistake in the commit description where we noted the allocation for 'copy' could fail, instead of 'filtered'. (Thanks Yujie!)
- Noted in the description that smatch also found this (Thanks Dan!)
- Added the extra Reported-by and Reviewed-by tags.
--- lib/kunit/executor.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
lib/kunit/executor.c +3 -1
@@ -74,8 +74,10 @@ kunit_filter_tests(const struct kunit_suite *const suite, const char *test_glob)return ERR_PTR (-ENOMEM ); filtered =kcalloc (n +1 ,sizeof (*filtered),GFP_KERNEL ); -if (!filtered) +if (!filtered) { +kfree (copy);return ERR_PTR (-ENOMEM ); + } n =0 ;kunit_suite_for_each_test_case (suite, test_case) {
Signature · 1 line
2.37.0.144.g8ac04bfd2-goog