提交 b2c1cb0a 编写于 作者: A Andrii Nakryiko 提交者: Zheng Zengkai

selftests/bpf: Fix GCC11 compiler warnings in -O2 mode

mainline inclusion
from mainline-5.18-rc1
commit d3b0b800
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I5EUVD
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d3b0b80064e0416850f818184b8f7bba9fdf8c40

-------------------------------------------------

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: NAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: NAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20220211190927.1434329-1-andrii@kernel.org
(cherry picked from commit d3b0b800)
Signed-off-by: NWang Yufen <wangyufen@huawei.com>

Conflicts:
	tools/testing/selftests/bpf/prog_tests/core_reloc.c
	tools/testing/selftests/bpf/prog_tests/tc_redirect.c
Signed-off-by: NWang Yufen <wangyufen@huawei.com>
上级 678cd0eb
...@@ -6111,7 +6111,7 @@ static int test_get_linfo(const struct prog_info_raw_test *test, ...@@ -6111,7 +6111,7 @@ static int test_get_linfo(const struct prog_info_raw_test *test,
static void do_test_info_raw(unsigned int test_num) static void do_test_info_raw(unsigned int test_num)
{ {
const struct prog_info_raw_test *test = &info_raw_tests[test_num - 1]; const struct prog_info_raw_test *test = &info_raw_tests[test_num - 1];
unsigned int raw_btf_size, linfo_str_off, linfo_size; unsigned int raw_btf_size, linfo_str_off, linfo_size = 0;
int btf_fd = -1, prog_fd = -1, err = 0; int btf_fd = -1, prog_fd = -1, err = 0;
void *raw_btf, *patched_linfo = NULL; void *raw_btf, *patched_linfo = NULL;
const char *ret_next_str; const char *ret_next_str;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册