Skip to content

Commit 8dcc628

Browse files
anakryikoYucong Sun
authored and
Yucong Sun
committed
selftests/bpf: fix GCC11 compiler warnings in -O2 mode
When compiling selftests in -O2 mode with GCC1, we get three new compilations warnings about potentially uninitialized variables. Compiler is wrong 2 out of 3 times, but this patch makes GCC11 happy anyways, as it doesn't cost us anything and makes optimized selftests build less annoying. The amazing one is tc_redirect case of token that is malloc()'ed before ASSERT_OK_PTR() check is done on it. Seems like GCC pessimistically assumes that libbpf_get_error() will dereference the contents of the pointer (no it won't), so the only way I found to shut GCC up was to do zero-initializaing calloc(). This one was new to me. For linfo case, GCC didn't realize that linfo_size will be initialized by the function that is returning linfo_size as out parameter. core_reloc.c case was a real bug, we can goto cleanup before initializing obj. But we don't need to do any clean up, so just continue iteration intstead. Signed-off-by: Andrii Nakryiko <[email protected]>
1 parent 7de5682 commit 8dcc628

File tree

3 files changed

+3
-3
lines changed

3 files changed

+3
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6556,7 +6556,7 @@ static int test_get_linfo(const struct prog_info_raw_test *test,
65566556
static void do_test_info_raw(unsigned int test_num)
65576557
{
65586558
const struct prog_info_raw_test *test = &info_raw_tests[test_num - 1];
6559-
unsigned int raw_btf_size, linfo_str_off, linfo_size;
6559+
unsigned int raw_btf_size, linfo_str_off, linfo_size = 0;
65606560
int btf_fd = -1, prog_fd = -1, err = 0;
65616561
void *raw_btf, *patched_linfo = NULL;
65626562
const char *ret_next_str;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ void test_core_reloc(void)
872872
if (test_case->btf_src_file) {
873873
err = access(test_case->btf_src_file, R_OK);
874874
if (!ASSERT_OK(err, "btf_src_file"))
875-
goto cleanup;
875+
continue;
876876
}
877877

878878
open_opts.btf_custom_path = test_case->btf_src_file;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ static struct nstoken *open_netns(const char *name)
140140
int err;
141141
struct nstoken *token;
142142

143-
token = malloc(sizeof(struct nstoken));
143+
token = calloc(1, sizeof(struct nstoken));
144144
if (!ASSERT_OK_PTR(token, "malloc token"))
145145
return NULL;
146146

0 commit comments

Comments
 (0)