selftests/bpf: Fix missing allocation null checks in test_progs.c

Add null checks after memory allocations to prevent potential segmentation faults.

Fixes: 79b4535013 ("tools/bpf: add a test for bpf_get_stack with tracepoint prog")
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-4-yangfeng59949@163.com
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
This commit is contained in:
Feng Yang 2026-07-23 16:50:58 +08:00 committed by Kumar Kartikeya Dwivedi
parent b04b8d4e19
commit 12b362b2f0
No known key found for this signature in database
GPG Key ID: 472D377B63542F83

View File

@ -730,11 +730,14 @@ int compare_map_keys(int map1_fd, int map2_fd)
int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len)
{
__u32 key, next_key, *cur_key_p, *next_key_p;
char *val_buf1, *val_buf2;
int i, err = 0;
char *val_buf1 = NULL, *val_buf2 = NULL;
int i, err = -ENOMEM;
val_buf1 = malloc(stack_trace_len);
val_buf2 = malloc(stack_trace_len);
if (!val_buf1 || !val_buf2)
goto out;
err = 0;
cur_key_p = NULL;
next_key_p = &key;
while (bpf_map_get_next_key(smap_fd, cur_key_p, next_key_p) == 0) {
@ -1514,6 +1517,10 @@ static int dispatch_thread_send_subtests(int sock_fd, struct test_state *state)
int subtest_num = state->subtest_num;
state->subtest_states = malloc(subtest_num * sizeof(*subtest_state));
if (!state->subtest_states) {
state->subtest_num = 0;
return -ENOMEM;
}
for (int i = 0; i < subtest_num; i++) {
subtest_state = &state->subtest_states[i];