diff mbox series

[v2,4/4] kunit: Fix possible memory leak in kunit_filter_suites()

Message ID 20230903071028.1518913-5-ruanjinjie@huawei.com (mailing list archive)
State Accepted
Commit 9076bc476d7ebf0565903c4b048442131825c1c3
Delegated to: Brendan Higgins
Headers show
Series kunit: Fix some bugs in kunit_filter_suites() | expand

Commit Message

Jinjie Ruan Sept. 3, 2023, 7:10 a.m. UTC
If both filter_glob and filters are not NULL, and kunit_parse_glob_filter()
succeed, but kcalloc parsed_filters fails, the suite_glob and test_glob of
parsed kzalloc in kunit_parse_glob_filter() will be leaked.

As Rae suggested, assign -ENOMEM to *err to correctly free copy and goto
free_parsed_glob to free the suite/test_glob of parsed.

Fixes: 1c9fd080dffe ("kunit: fix uninitialized variables bug in attributes filtering")
Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
Suggested-by: Rae Moar <rmoar@google.com>
---
v2:
- Add *err = -ENOMEM before goto to correctly free copy.
- Goto the new add identical purpose free_parsed_glob label.
- Update the commit message.
---
 lib/kunit/executor.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

David Gow Sept. 5, 2023, 7:13 a.m. UTC | #1
On Sun, 3 Sept 2023 at 15:11, 'Jinjie Ruan' via KUnit Development
<kunit-dev@googlegroups.com> wrote:
>
> If both filter_glob and filters are not NULL, and kunit_parse_glob_filter()
> succeed, but kcalloc parsed_filters fails, the suite_glob and test_glob of
> parsed kzalloc in kunit_parse_glob_filter() will be leaked.
>
> As Rae suggested, assign -ENOMEM to *err to correctly free copy and goto
> free_parsed_glob to free the suite/test_glob of parsed.
>
> Fixes: 1c9fd080dffe ("kunit: fix uninitialized variables bug in attributes filtering")
> Signed-off-by: Jinjie Ruan <ruanjinjie@huawei.com>
> Suggested-by: Rae Moar <rmoar@google.com>
> ---
> v2:
> - Add *err = -ENOMEM before goto to correctly free copy.
> - Goto the new add identical purpose free_parsed_glob label.
> - Update the commit message.
> ---

Looks good to me.

Reviewed-by: David Gow <davidgow@google.com>

Cheers,
-- David


>  lib/kunit/executor.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
> index 28f144de748b..a6348489d45f 100644
> --- a/lib/kunit/executor.c
> +++ b/lib/kunit/executor.c
> @@ -175,8 +175,8 @@ kunit_filter_suites(const struct kunit_suite_set *suite_set,
>                 filter_count = kunit_get_filter_count(filters);
>                 parsed_filters = kcalloc(filter_count, sizeof(*parsed_filters), GFP_KERNEL);
>                 if (!parsed_filters) {
> -                       kfree(copy);
> -                       return filtered;
> +                       *err = -ENOMEM;
> +                       goto free_parsed_glob;
>                 }
>                 for (j = 0; j < filter_count; j++)
>                         parsed_filters[j] = kunit_next_attr_filter(&filters, err);
> --
> 2.34.1
>
> --
> You received this message because you are subscribed to the Google Groups "KUnit Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to kunit-dev+unsubscribe@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/20230903071028.1518913-5-ruanjinjie%40huawei.com.
diff mbox series

Patch

diff --git a/lib/kunit/executor.c b/lib/kunit/executor.c
index 28f144de748b..a6348489d45f 100644
--- a/lib/kunit/executor.c
+++ b/lib/kunit/executor.c
@@ -175,8 +175,8 @@  kunit_filter_suites(const struct kunit_suite_set *suite_set,
 		filter_count = kunit_get_filter_count(filters);
 		parsed_filters = kcalloc(filter_count, sizeof(*parsed_filters), GFP_KERNEL);
 		if (!parsed_filters) {
-			kfree(copy);
-			return filtered;
+			*err = -ENOMEM;
+			goto free_parsed_glob;
 		}
 		for (j = 0; j < filter_count; j++)
 			parsed_filters[j] = kunit_next_attr_filter(&filters, err);