From: Feng Yang <[email protected]>

Fix memory leak in subtest_states reallocation,
and revert subtest_num if allocation fails.

Fixes: 0925225956bb ("bpf/selftests: Add granular subtest output for prog_test")
Signed-off-by: Feng Yang <[email protected]>
---
 tools/testing/selftests/bpf/test_progs.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/bpf/test_progs.c 
b/tools/testing/selftests/bpf/test_progs.c
index ed9ad7ad3352..ea9c887201ae 100644
--- a/tools/testing/selftests/bpf/test_progs.c
+++ b/tools/testing/selftests/bpf/test_progs.c
@@ -573,18 +573,19 @@ bool test__start_subtest_with_desc(const char 
*subtest_name, const char *subtest
        struct subtest_state *subtest_state;
        const char *subtest_display_name;
        size_t sub_state_size = sizeof(*subtest_state);
+       void *tmp;
 
        if (env.subtest_state)
                test__end_subtest();
 
        state->subtest_num++;
-       state->subtest_states =
-               realloc(state->subtest_states,
-                       state->subtest_num * sub_state_size);
-       if (!state->subtest_states) {
+       tmp = realloc(state->subtest_states, state->subtest_num * 
sub_state_size);
+       if (!tmp) {
+               state->subtest_num--;
                fprintf(stderr, "Not enough memory to allocate subtest 
result\n");
                return false;
        }
+       state->subtest_states = tmp;
 
        subtest_state = &state->subtest_states[state->subtest_num - 1];
 
-- 
2.43.0


Reply via email to