From: Feng Yang <[email protected]>

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

Fixes: 79b453501310 ("tools/bpf: add a test for bpf_get_stack with tracepoint 
prog")
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 | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/bpf/test_progs.c 
b/tools/testing/selftests/bpf/test_progs.c
index 312743c4337f..301c6e11ceaf 100644
--- a/tools/testing/selftests/bpf/test_progs.c
+++ b/tools/testing/selftests/bpf/test_progs.c
@@ -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];
-- 
2.43.0


Reply via email to