Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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

Fix a bunch of potentially unitialized variable usage warnings that are
reported by GCC in -O2 mode. Also silence overzealous stringop-truncation
class of warnings.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Link: https://lore.kernel.org/bpf/20231006175744.3136675-1-andrii@kernel.org

authored by

Andrii Nakryiko and committed by
Daniel Borkmann
925a0157 bc5bc309

+27 -24
+3 -1
tools/testing/selftests/bpf/Makefile
··· 27 27 BPF_GCC ?= $(shell command -v bpf-gcc;) 28 28 SAN_CFLAGS ?= 29 29 SAN_LDFLAGS ?= $(SAN_CFLAGS) 30 - CFLAGS += -g -O0 -rdynamic -Wall -Werror $(GENFLAGS) $(SAN_CFLAGS) \ 30 + CFLAGS += -g -O0 -rdynamic \ 31 + -Wall -Werror \ 32 + $(GENFLAGS) $(SAN_CFLAGS) \ 31 33 -I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \ 32 34 -I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT) 33 35 LDFLAGS += $(SAN_LDFLAGS)
+2 -2
tools/testing/selftests/bpf/map_tests/map_in_map_batch_ops.c
··· 33 33 { 34 34 int map_fd, map_index, ret; 35 35 __u32 map_key = 0, map_id; 36 - char map_name[15]; 36 + char map_name[16]; 37 37 38 38 for (map_index = 0; map_index < OUTER_MAP_ENTRIES; map_index++) { 39 39 memset(map_name, 0, sizeof(map_name)); 40 - sprintf(map_name, "inner_map_fd_%d", map_index); 40 + snprintf(map_name, sizeof(map_name), "inner_map_fd_%d", map_index); 41 41 map_fd = bpf_map_create(map_type, map_name, sizeof(__u32), 42 42 sizeof(__u32), 1, NULL); 43 43 CHECK(map_fd < 0,
+2 -2
tools/testing/selftests/bpf/prog_tests/bloom_filter_map.c
··· 193 193 194 194 void test_bloom_filter_map(void) 195 195 { 196 - __u32 *rand_vals, nr_rand_vals; 197 - struct bloom_filter_map *skel; 196 + __u32 *rand_vals = NULL, nr_rand_vals = 0; 197 + struct bloom_filter_map *skel = NULL; 198 198 int err; 199 199 200 200 test_fail_cases();
+2 -2
tools/testing/selftests/bpf/prog_tests/connect_ping.c
··· 28 28 .sin6_family = AF_INET6, 29 29 .sin6_addr = IN6ADDR_LOOPBACK_INIT, 30 30 }; 31 - struct sockaddr *sa; 31 + struct sockaddr *sa = NULL; 32 32 socklen_t sa_len; 33 - int protocol; 33 + int protocol = -1; 34 34 int sock_fd; 35 35 36 36 switch (family) {
+1 -1
tools/testing/selftests/bpf/prog_tests/linked_list.c
··· 268 268 269 269 static void list_and_rb_node_same_struct(bool refcount_field) 270 270 { 271 - int bpf_rb_node_btf_id, bpf_refcount_btf_id, foo_btf_id; 271 + int bpf_rb_node_btf_id, bpf_refcount_btf_id = 0, foo_btf_id; 272 272 struct btf *btf; 273 273 int id, err; 274 274
+2 -1
tools/testing/selftests/bpf/prog_tests/lwt_helpers.h
··· 49 49 return -1; 50 50 51 51 ifr.ifr_flags = IFF_NO_PI | (need_mac ? IFF_TAP : IFF_TUN); 52 - memcpy(ifr.ifr_name, dev_name, IFNAMSIZ); 52 + strncpy(ifr.ifr_name, dev_name, IFNAMSIZ - 1); 53 + ifr.ifr_name[IFNAMSIZ - 1] = '\0'; 53 54 54 55 err = ioctl(fd, TUNSETIFF, &ifr); 55 56 if (!ASSERT_OK(err, "ioctl(TUNSETIFF)")) {
+1 -1
tools/testing/selftests/bpf/prog_tests/queue_stack_map.c
··· 14 14 int i, err, prog_fd, map_in_fd, map_out_fd; 15 15 char file[32], buf[128]; 16 16 struct bpf_object *obj; 17 - struct iphdr iph; 17 + struct iphdr iph = {}; 18 18 LIBBPF_OPTS(bpf_test_run_opts, topts, 19 19 .data_in = &pkt_v4, 20 20 .data_size_in = sizeof(pkt_v4),
+4 -4
tools/testing/selftests/bpf/prog_tests/sockmap_basic.c
··· 359 359 static void test_sockmap_skb_verdict_shutdown(void) 360 360 { 361 361 struct epoll_event ev, events[MAX_EVENTS]; 362 - int n, err, map, verdict, s, c1, p1; 362 + int n, err, map, verdict, s, c1 = -1, p1 = -1; 363 363 struct test_sockmap_pass_prog *skel; 364 364 int epollfd; 365 365 int zero = 0; ··· 414 414 static void test_sockmap_skb_verdict_fionread(bool pass_prog) 415 415 { 416 416 int expected, zero = 0, sent, recvd, avail; 417 - int err, map, verdict, s, c0, c1, p0, p1; 418 - struct test_sockmap_pass_prog *pass; 419 - struct test_sockmap_drop_prog *drop; 417 + int err, map, verdict, s, c0 = -1, c1 = -1, p0 = -1, p1 = -1; 418 + struct test_sockmap_pass_prog *pass = NULL; 419 + struct test_sockmap_drop_prog *drop = NULL; 420 420 char buf[256] = "0123456789"; 421 421 422 422 if (pass_prog) {
+1 -1
tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h
··· 378 378 static inline int socket_loopback_reuseport(int family, int sotype, int progfd) 379 379 { 380 380 struct sockaddr_storage addr; 381 - socklen_t len; 381 + socklen_t len = 0; 382 382 int err, s; 383 383 384 384 init_addr_loopback(family, &addr, &len);
+2 -2
tools/testing/selftests/bpf/prog_tests/sockmap_listen.c
··· 73 73 int family, int sotype, int mapfd) 74 74 { 75 75 struct sockaddr_storage addr; 76 - socklen_t len; 76 + socklen_t len = 0; 77 77 u32 key = 0; 78 78 u64 value; 79 79 int err, s; ··· 871 871 872 872 static void redir_partial(int family, int sotype, int sock_map, int parser_map) 873 873 { 874 - int s, c0, c1, p0, p1; 874 + int s, c0 = -1, c1 = -1, p0 = -1, p1 = -1; 875 875 int err, n, key, value; 876 876 char buf[] = "abc"; 877 877
+1 -1
tools/testing/selftests/bpf/prog_tests/xdp_metadata.c
··· 226 226 __u64 comp_addr; 227 227 void *data; 228 228 __u64 addr; 229 - __u32 idx; 229 + __u32 idx = 0; 230 230 int ret; 231 231 232 232 ret = recvfrom(xsk_socket__fd(xsk->socket), NULL, 0, MSG_DONTWAIT, NULL, NULL);
+2 -2
tools/testing/selftests/bpf/test_loader.c
··· 69 69 { 70 70 if (!tester->log_buf) { 71 71 tester->log_buf_sz = TEST_LOADER_LOG_BUF_SZ; 72 - tester->log_buf = malloc(tester->log_buf_sz); 72 + tester->log_buf = calloc(tester->log_buf_sz, 1); 73 73 if (!ASSERT_OK_PTR(tester->log_buf, "tester_log_buf")) 74 74 return -ENOMEM; 75 75 } ··· 538 538 bool unpriv) 539 539 { 540 540 struct test_subspec *subspec = unpriv ? &spec->unpriv : &spec->priv; 541 - struct bpf_program *tprog, *tprog_iter; 541 + struct bpf_program *tprog = NULL, *tprog_iter; 542 542 struct test_spec *spec_iter; 543 543 struct cap_state caps = {}; 544 544 struct bpf_object *tobj;
+2 -2
tools/testing/selftests/bpf/xdp_features.c
··· 360 360 static int dut_run(struct xdp_features *skel) 361 361 { 362 362 int flags = XDP_FLAGS_UPDATE_IF_NOEXIST | XDP_FLAGS_DRV_MODE; 363 - int state, err, *sockfd, ctrl_sockfd, echo_sockfd; 363 + int state, err = 0, *sockfd, ctrl_sockfd, echo_sockfd; 364 364 struct sockaddr_storage ctrl_addr; 365 - pthread_t dut_thread; 365 + pthread_t dut_thread = 0; 366 366 socklen_t addrlen; 367 367 368 368 sockfd = start_reuseport_server(AF_INET6, SOCK_STREAM, NULL,
+1 -1
tools/testing/selftests/bpf/xdp_hw_metadata.c
··· 234 234 struct pollfd fds[rxq + 1]; 235 235 __u64 comp_addr; 236 236 __u64 addr; 237 - __u32 idx; 237 + __u32 idx = 0; 238 238 int ret; 239 239 int i; 240 240
+1 -1
tools/testing/selftests/bpf/xskxceiver.c
··· 1049 1049 struct xsk_umem_info *umem = xsk->umem; 1050 1050 struct pollfd fds = { }; 1051 1051 struct pkt *pkt; 1052 - u64 first_addr; 1052 + u64 first_addr = 0; 1053 1053 int ret; 1054 1054 1055 1055 fds.fd = xsk_socket__fd(xsk->xsk);