* [PATCH] kunit: executor: Fix a memory leak on failure in kunit_filter_tests
@ 2022-07-12 9:56 David Gow
2022-07-12 17:54 ` Yujie Liu
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: David Gow @ 2022-07-12 9:56 UTC (permalink / raw)
To: Daniel Latypov, Brendan Higgins, Shuah Khan
Cc: David Gow, kunit-dev, linux-kselftest, Linux Kernel Mailing List,
llvm, kernel test robot
It's possible that memory allocation for the copy 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/c8073b8e-7b9e-0830-4177-87c12f16349c@intel.com/
Fixes: a02353f49162 ("kunit: bail out of test filtering logic quicker if OOM")
Reported-by: kernel test robot <yujie.liu@intel.com>
Signed-off-by: David Gow <davidgow@google.com>
---
lib/kunit/executor.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
index 6c489d6c5e5d..5e223327196a 100644
--- a/lib/kunit/executor.c
+++ b/lib/kunit/executor.c
@@ -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) {
--
2.37.0.144.g8ac04bfd2-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] kunit: executor: Fix a memory leak on failure in kunit_filter_tests
2022-07-12 9:56 [PATCH] kunit: executor: Fix a memory leak on failure in kunit_filter_tests David Gow
@ 2022-07-12 17:54 ` Yujie Liu
2022-07-12 21:27 ` Daniel Latypov
2022-07-12 21:30 ` Brendan Higgins
2 siblings, 0 replies; 4+ messages in thread
From: Yujie Liu @ 2022-07-12 17:54 UTC (permalink / raw)
To: David Gow
Cc: kunit-dev, linux-kselftest, Linux Kernel Mailing List, llvm,
Shuah Khan, Brendan Higgins, Daniel Latypov
Hi David,
On 7/12/2022 17:56, David Gow wrote:
> It's possible that memory allocation for the copy will fail, but for the
I think it is "for the _filtered_ will fail". Maybe a typo?
Thanks,
Yujie
> 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/c8073b8e-7b9e-0830-4177-87c12f16349c@intel.com/
>
> Fixes: a02353f49162 ("kunit: bail out of test filtering logic quicker if OOM")
> Reported-by: kernel test robot <yujie.liu@intel.com>
> Signed-off-by: David Gow <davidgow@google.com>
> ---
> lib/kunit/executor.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
> index 6c489d6c5e5d..5e223327196a 100644
> --- a/lib/kunit/executor.c
> +++ b/lib/kunit/executor.c
> @@ -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) {
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] kunit: executor: Fix a memory leak on failure in kunit_filter_tests
2022-07-12 9:56 [PATCH] kunit: executor: Fix a memory leak on failure in kunit_filter_tests David Gow
2022-07-12 17:54 ` Yujie Liu
@ 2022-07-12 21:27 ` Daniel Latypov
2022-07-12 21:30 ` Brendan Higgins
2 siblings, 0 replies; 4+ messages in thread
From: Daniel Latypov @ 2022-07-12 21:27 UTC (permalink / raw)
To: David Gow
Cc: Brendan Higgins, Shuah Khan, kunit-dev, linux-kselftest,
Linux Kernel Mailing List, llvm, kernel test robot
On Tue, Jul 12, 2022 at 2:56 AM David Gow <davidgow@google.com> wrote:
>
> It's possible that memory allocation for the copy 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/c8073b8e-7b9e-0830-4177-87c12f16349c@intel.com/
>
> Fixes: a02353f49162 ("kunit: bail out of test filtering logic quicker if OOM")
> Reported-by: kernel test robot <yujie.liu@intel.com>
> Signed-off-by: David Gow <davidgow@google.com>
Reviewed-by: Daniel Latypov <dlatypov@google.com>
Thanks for taking care of this.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] kunit: executor: Fix a memory leak on failure in kunit_filter_tests
2022-07-12 9:56 [PATCH] kunit: executor: Fix a memory leak on failure in kunit_filter_tests David Gow
2022-07-12 17:54 ` Yujie Liu
2022-07-12 21:27 ` Daniel Latypov
@ 2022-07-12 21:30 ` Brendan Higgins
2 siblings, 0 replies; 4+ messages in thread
From: Brendan Higgins @ 2022-07-12 21:30 UTC (permalink / raw)
To: David Gow
Cc: Daniel Latypov, Shuah Khan, kunit-dev, linux-kselftest,
Linux Kernel Mailing List, llvm, kernel test robot
On Tue, Jul 12, 2022 at 5:56 AM David Gow <davidgow@google.com> wrote:
>
> It's possible that memory allocation for the copy 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/c8073b8e-7b9e-0830-4177-87c12f16349c@intel.com/
>
> Fixes: a02353f49162 ("kunit: bail out of test filtering logic quicker if OOM")
> Reported-by: kernel test robot <yujie.liu@intel.com>
> Signed-off-by: David Gow <davidgow@google.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-07-12 21:30 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-07-12 9:56 [PATCH] kunit: executor: Fix a memory leak on failure in kunit_filter_tests David Gow
2022-07-12 17:54 ` Yujie Liu
2022-07-12 21:27 ` Daniel Latypov
2022-07-12 21:30 ` Brendan Higgins
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox