Skip to content

Commit bf4a64b

Browse files
yuranpereiraAlexei Starovoitov
authored andcommitted
selftests/bpf: Add malloc failure checks in bpf_iter
Since some malloc calls in bpf_iter may at times fail, this patch adds the appropriate fail checks, and ensures that any previously allocated resource is appropriately destroyed before returning the function. Signed-off-by: Yuran Pereira <[email protected]> Acked-by: Yonghong Song <[email protected]> Acked-by: Kui-Feng Lee <[email protected]> Link: https://lore.kernel.org/r/DB3PR10MB6835F0ECA792265FA41FC39BE8A3A@DB3PR10MB6835.EURPRD10.PROD.OUTLOOK.COM Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent fac85c2 commit bf4a64b

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

tools/testing/selftests/bpf/prog_tests/bpf_iter.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ static void test_overflow(bool test_e2big_overflow, bool ret1)
698698
goto free_link;
699699

700700
buf = malloc(expected_read_len);
701-
if (!buf)
701+
if (!ASSERT_OK_PTR(buf, "malloc"))
702702
goto close_iter;
703703

704704
/* do read */
@@ -868,6 +868,8 @@ static void test_bpf_percpu_hash_map(void)
868868

869869
skel->rodata->num_cpus = bpf_num_possible_cpus();
870870
val = malloc(8 * bpf_num_possible_cpus());
871+
if (!ASSERT_OK_PTR(val, "malloc"))
872+
goto out;
871873

872874
err = bpf_iter_bpf_percpu_hash_map__load(skel);
873875
if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_percpu_hash_map__load"))
@@ -1044,6 +1046,8 @@ static void test_bpf_percpu_array_map(void)
10441046

10451047
skel->rodata->num_cpus = bpf_num_possible_cpus();
10461048
val = malloc(8 * bpf_num_possible_cpus());
1049+
if (!ASSERT_OK_PTR(val, "malloc"))
1050+
goto out;
10471051

10481052
err = bpf_iter_bpf_percpu_array_map__load(skel);
10491053
if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_percpu_array_map__load"))

0 commit comments

Comments
 (0)