selftests/bpf: Fix memory leak on subtest_states reallocation

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

Fixes: 0925225956 ("bpf/selftests: Add granular subtest output for prog_test")
Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
Link: https://lore.kernel.org/bpf/20260723085100.482147-6-yangfeng59949@163.com
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
This commit is contained in:
Feng Yang
2026-07-23 16:51:00 +08:00
committed by Kumar Kartikeya Dwivedi
parent a813ad2185
commit 06efb01c65

View File

@@ -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];