Skip to content

Commit 925a015

Browse files
anakryikoborkmann
authored andcommitted
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
1 parent bc5bc30 commit 925a015

15 files changed

Lines changed: 27 additions & 24 deletions

tools/testing/selftests/bpf/Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ endif
2727
BPF_GCC ?= $(shell command -v bpf-gcc;)
2828
SAN_CFLAGS ?=
2929
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) \
3133
-I$(CURDIR) -I$(INCLUDE_DIR) -I$(GENDIR) -I$(LIBDIR) \
3234
-I$(TOOLSINCDIR) -I$(APIDIR) -I$(OUTPUT)
3335
LDFLAGS += $(SAN_LDFLAGS)

tools/testing/selftests/bpf/map_tests/map_in_map_batch_ops.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ static void create_inner_maps(enum bpf_map_type map_type,
3333
{
3434
int map_fd, map_index, ret;
3535
__u32 map_key = 0, map_id;
36-
char map_name[15];
36+
char map_name[16];
3737

3838
for (map_index = 0; map_index < OUTER_MAP_ENTRIES; map_index++) {
3939
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);
4141
map_fd = bpf_map_create(map_type, map_name, sizeof(__u32),
4242
sizeof(__u32), 1, NULL);
4343
CHECK(map_fd < 0,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ static int setup_progs(struct bloom_filter_map **out_skel, __u32 **out_rand_vals
193193

194194
void test_bloom_filter_map(void)
195195
{
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;
198198
int err;
199199

200200
test_fail_cases();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ static void subtest(int cgroup_fd, struct connect_ping *skel,
2828
.sin6_family = AF_INET6,
2929
.sin6_addr = IN6ADDR_LOOPBACK_INIT,
3030
};
31-
struct sockaddr *sa;
31+
struct sockaddr *sa = NULL;
3232
socklen_t sa_len;
33-
int protocol;
33+
int protocol = -1;
3434
int sock_fd;
3535

3636
switch (family) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ static struct btf *init_btf(void)
268268

269269
static void list_and_rb_node_same_struct(bool refcount_field)
270270
{
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;
272272
struct btf *btf;
273273
int id, err;
274274

tools/testing/selftests/bpf/prog_tests/lwt_helpers.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ static int open_tuntap(const char *dev_name, bool need_mac)
4949
return -1;
5050

5151
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';
5354

5455
err = ioctl(fd, TUNSETIFF, &ifr);
5556
if (!ASSERT_OK(err, "ioctl(TUNSETIFF)")) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ static void test_queue_stack_map_by_type(int type)
1414
int i, err, prog_fd, map_in_fd, map_out_fd;
1515
char file[32], buf[128];
1616
struct bpf_object *obj;
17-
struct iphdr iph;
17+
struct iphdr iph = {};
1818
LIBBPF_OPTS(bpf_test_run_opts, topts,
1919
.data_in = &pkt_v4,
2020
.data_size_in = sizeof(pkt_v4),

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ static void test_sockmap_progs_query(enum bpf_attach_type attach_type)
359359
static void test_sockmap_skb_verdict_shutdown(void)
360360
{
361361
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;
363363
struct test_sockmap_pass_prog *skel;
364364
int epollfd;
365365
int zero = 0;
@@ -414,9 +414,9 @@ static void test_sockmap_skb_verdict_shutdown(void)
414414
static void test_sockmap_skb_verdict_fionread(bool pass_prog)
415415
{
416416
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;
420420
char buf[256] = "0123456789";
421421

422422
if (pass_prog) {

tools/testing/selftests/bpf/prog_tests/sockmap_helpers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ static inline int enable_reuseport(int s, int progfd)
378378
static inline int socket_loopback_reuseport(int family, int sotype, int progfd)
379379
{
380380
struct sockaddr_storage addr;
381-
socklen_t len;
381+
socklen_t len = 0;
382382
int err, s;
383383

384384
init_addr_loopback(family, &addr, &len);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static void test_insert_bound(struct test_sockmap_listen *skel __always_unused,
7373
int family, int sotype, int mapfd)
7474
{
7575
struct sockaddr_storage addr;
76-
socklen_t len;
76+
socklen_t len = 0;
7777
u32 key = 0;
7878
u64 value;
7979
int err, s;
@@ -871,7 +871,7 @@ static void test_msg_redir_to_listening(struct test_sockmap_listen *skel,
871871

872872
static void redir_partial(int family, int sotype, int sock_map, int parser_map)
873873
{
874-
int s, c0, c1, p0, p1;
874+
int s, c0 = -1, c1 = -1, p0 = -1, p1 = -1;
875875
int err, n, key, value;
876876
char buf[] = "abc";
877877

0 commit comments

Comments
 (0)