Skip to content

Commit f3f8649

Browse files
committed
Merge branch 'bpftool-using-the-right-format-specifiers'
Jiayuan Chen says: ==================== bpftool: Using the right format specifiers This patch adds the -Wformat-signedness compiler flag to detect and prevent format string errors, where signed or unsigned types are mismatched with format specifiers. Additionally, it fixes some format string errors that were not fully addressed by the previous patch [1]. [1] https://lore.kernel.org/bpf/20250207123706.727928-1-mrpre@163.com/T/#u --- v1->v2: https://lore.kernel.org/bpf/20250310142037.45932-1-jiayuan.chen@linux.dev/ --- ==================== Link: https://patch.msgid.link/20250311112809.81901-1-jiayuan.chen@linux.dev Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
2 parents b02f072 + 3775be3 commit f3f8649

File tree

13 files changed

+39
-33
lines changed

13 files changed

+39
-33
lines changed

kernel/bpf/disasm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,
202202
insn->dst_reg, class == BPF_ALU ? 'w' : 'r',
203203
insn->dst_reg);
204204
} else if (is_addr_space_cast(insn)) {
205-
verbose(cbs->private_data, "(%02x) r%d = addr_space_cast(r%d, %d, %d)\n",
205+
verbose(cbs->private_data, "(%02x) r%d = addr_space_cast(r%d, %u, %u)\n",
206206
insn->code, insn->dst_reg,
207207
insn->src_reg, ((u32)insn->imm) >> 16, (u16)insn->imm);
208208
} else if (is_mov_percpu_addr(insn)) {
@@ -381,7 +381,7 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,
381381
insn->code, class == BPF_JMP32 ? 'w' : 'r',
382382
insn->dst_reg,
383383
bpf_jmp_string[BPF_OP(insn->code) >> 4],
384-
insn->imm, insn->off);
384+
(u32)insn->imm, insn->off);
385385
}
386386
} else {
387387
verbose(cbs->private_data, "(%02x) %s\n",

tools/bpf/bpftool/Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,12 @@ prefix ?= /usr/local
7171
bash_compdir ?= /usr/share/bash-completion/completions
7272

7373
CFLAGS += -O2
74-
CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
74+
CFLAGS += -W
75+
CFLAGS += -Wall
76+
CFLAGS += -Wextra
77+
CFLAGS += -Wformat-signedness
78+
CFLAGS += -Wno-unused-parameter
79+
CFLAGS += -Wno-missing-field-initializers
7580
CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
7681
CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
7782
-I$(or $(OUTPUT),.) \

tools/bpf/bpftool/btf.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ static int dump_btf_type(const struct btf *btf, __u32 id,
253253
if (btf_kflag(t))
254254
printf("\n\t'%s' val=%d", name, v->val);
255255
else
256-
printf("\n\t'%s' val=%u", name, v->val);
256+
printf("\n\t'%s' val=%u", name, (__u32)v->val);
257257
}
258258
}
259259
if (json_output)
@@ -1022,7 +1022,7 @@ static int do_dump(int argc, char **argv)
10221022
for (i = 0; i < root_type_cnt; i++) {
10231023
if (root_type_ids[i] == root_id) {
10241024
err = -EINVAL;
1025-
p_err("duplicate root_id %d supplied", root_id);
1025+
p_err("duplicate root_id %u supplied", root_id);
10261026
goto done;
10271027
}
10281028
}
@@ -1132,7 +1132,7 @@ build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type,
11321132
break;
11331133
default:
11341134
err = -1;
1135-
p_err("unexpected object type: %d", type);
1135+
p_err("unexpected object type: %u", type);
11361136
goto err_free;
11371137
}
11381138
if (err) {
@@ -1155,7 +1155,7 @@ build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type,
11551155
break;
11561156
default:
11571157
err = -1;
1158-
p_err("unexpected object type: %d", type);
1158+
p_err("unexpected object type: %u", type);
11591159
goto err_free;
11601160
}
11611161
if (fd < 0) {
@@ -1188,7 +1188,7 @@ build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type,
11881188
break;
11891189
default:
11901190
err = -1;
1191-
p_err("unexpected object type: %d", type);
1191+
p_err("unexpected object type: %u", type);
11921192
goto err_free;
11931193
}
11941194
if (!btf_id)
@@ -1254,12 +1254,12 @@ show_btf_plain(struct bpf_btf_info *info, int fd,
12541254

12551255
n = 0;
12561256
hashmap__for_each_key_entry(btf_prog_table, entry, info->id) {
1257-
printf("%s%lu", n++ == 0 ? " prog_ids " : ",", entry->value);
1257+
printf("%s%lu", n++ == 0 ? " prog_ids " : ",", (unsigned long)entry->value);
12581258
}
12591259

12601260
n = 0;
12611261
hashmap__for_each_key_entry(btf_map_table, entry, info->id) {
1262-
printf("%s%lu", n++ == 0 ? " map_ids " : ",", entry->value);
1262+
printf("%s%lu", n++ == 0 ? " map_ids " : ",", (unsigned long)entry->value);
12631263
}
12641264

12651265
emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");

tools/bpf/bpftool/btf_dumper.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id,
653653
case BTF_KIND_ARRAY:
654654
array = (struct btf_array *)(t + 1);
655655
BTF_PRINT_TYPE(array->type);
656-
BTF_PRINT_ARG("[%d]", array->nelems);
656+
BTF_PRINT_ARG("[%u]", array->nelems);
657657
break;
658658
case BTF_KIND_PTR:
659659
BTF_PRINT_TYPE(t->type);

tools/bpf/bpftool/cgroup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ static int show_bpf_prog(int id, enum bpf_attach_type attach_type,
191191
if (attach_btf_name)
192192
printf(" %-15s", attach_btf_name);
193193
else if (info.attach_btf_id)
194-
printf(" attach_btf_obj_id=%d attach_btf_id=%d",
194+
printf(" attach_btf_obj_id=%u attach_btf_id=%u",
195195
info.attach_btf_obj_id, info.attach_btf_id);
196196
printf("\n");
197197
}

tools/bpf/bpftool/common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt)
714714
int vendor_id;
715715

716716
if (!ifindex_to_name_ns(ifindex, ns_dev, ns_ino, devname)) {
717-
p_err("Can't get net device name for ifindex %d: %s", ifindex,
717+
p_err("Can't get net device name for ifindex %u: %s", ifindex,
718718
strerror(errno));
719719
return NULL;
720720
}
@@ -739,7 +739,7 @@ ifindex_to_arch(__u32 ifindex, __u64 ns_dev, __u64 ns_ino, const char **opt)
739739
/* No NFP support in LLVM, we have no valid triple to return. */
740740
default:
741741
p_err("Can't get arch name for device vendor id 0x%04x",
742-
vendor_id);
742+
(unsigned int)vendor_id);
743743
return NULL;
744744
}
745745
}

tools/bpf/bpftool/jit_disasm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,8 @@ int disasm_print_insn(unsigned char *image, ssize_t len, int opcodes,
343343
{
344344
const struct bpf_line_info *linfo = NULL;
345345
unsigned int nr_skip = 0;
346-
int count, i, pc = 0;
346+
int count, i;
347+
unsigned int pc = 0;
347348
disasm_ctx_t ctx;
348349

349350
if (!len)

tools/bpf/bpftool/map_perf_ring.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,15 @@ print_bpf_output(void *private_data, int cpu, struct perf_event_header *event)
9191
jsonw_end_object(json_wtr);
9292
} else {
9393
if (e->header.type == PERF_RECORD_SAMPLE) {
94-
printf("== @%lld.%09lld CPU: %d index: %d =====\n",
94+
printf("== @%llu.%09llu CPU: %d index: %d =====\n",
9595
e->time / 1000000000ULL, e->time % 1000000000ULL,
9696
cpu, idx);
9797
fprint_hex(stdout, e->data, e->size, " ");
9898
printf("\n");
9999
} else if (e->header.type == PERF_RECORD_LOST) {
100-
printf("lost %lld events\n", lost->lost);
100+
printf("lost %llu events\n", lost->lost);
101101
} else {
102-
printf("unknown event type=%d size=%d\n",
102+
printf("unknown event type=%u size=%u\n",
103103
e->header.type, e->header.size);
104104
}
105105
}

tools/bpf/bpftool/net.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ static void __show_dev_tc_bpf(const struct ip_devname_ifindex *dev,
476476
for (i = 0; i < optq.count; i++) {
477477
NET_START_OBJECT;
478478
NET_DUMP_STR("devname", "%s", dev->devname);
479-
NET_DUMP_UINT("ifindex", "(%u)", dev->ifindex);
479+
NET_DUMP_UINT("ifindex", "(%u)", (unsigned int)dev->ifindex);
480480
NET_DUMP_STR("kind", " %s", attach_loc_strings[loc]);
481481
ret = __show_dev_tc_bpf_name(prog_ids[i], prog_name,
482482
sizeof(prog_name));
@@ -831,7 +831,7 @@ static void show_link_netfilter(void)
831831
if (err) {
832832
if (errno == ENOENT)
833833
break;
834-
p_err("can't get next link: %s (id %d)", strerror(errno), id);
834+
p_err("can't get next link: %s (id %u)", strerror(errno), id);
835835
break;
836836
}
837837

tools/bpf/bpftool/netlink_dumper.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static int do_xdp_dump_one(struct nlattr *attr, unsigned int ifindex,
4545
NET_START_OBJECT;
4646
if (name)
4747
NET_DUMP_STR("devname", "%s", name);
48-
NET_DUMP_UINT("ifindex", "(%d)", ifindex);
48+
NET_DUMP_UINT("ifindex", "(%u)", ifindex);
4949

5050
if (mode == XDP_ATTACHED_MULTI) {
5151
if (json_output) {
@@ -74,7 +74,7 @@ int do_xdp_dump(struct ifinfomsg *ifinfo, struct nlattr **tb)
7474
if (!tb[IFLA_XDP])
7575
return 0;
7676

77-
return do_xdp_dump_one(tb[IFLA_XDP], ifinfo->ifi_index,
77+
return do_xdp_dump_one(tb[IFLA_XDP], (unsigned int)ifinfo->ifi_index,
7878
libbpf_nla_getattr_str(tb[IFLA_IFNAME]));
7979
}
8080

@@ -168,7 +168,7 @@ int do_filter_dump(struct tcmsg *info, struct nlattr **tb, const char *kind,
168168
NET_START_OBJECT;
169169
if (devname[0] != '\0')
170170
NET_DUMP_STR("devname", "%s", devname);
171-
NET_DUMP_UINT("ifindex", "(%u)", ifindex);
171+
NET_DUMP_UINT("ifindex", "(%u)", (unsigned int)ifindex);
172172
NET_DUMP_STR("kind", " %s", kind);
173173
ret = do_bpf_filter_dump(tb[TCA_OPTIONS]);
174174
NET_END_OBJECT_FINAL;

0 commit comments

Comments
 (0)