Commit f3f86495 authored by Andrii Nakryiko's avatar Andrii Nakryiko
Browse files

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: default avatarAndrii Nakryiko <andrii@kernel.org>
parents b02f072a 3775be34
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -202,7 +202,7 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,
				insn->dst_reg, class == BPF_ALU ? 'w' : 'r',
				insn->dst_reg);
		} else if (is_addr_space_cast(insn)) {
			verbose(cbs->private_data, "(%02x) r%d = addr_space_cast(r%d, %d, %d)\n",
			verbose(cbs->private_data, "(%02x) r%d = addr_space_cast(r%d, %u, %u)\n",
				insn->code, insn->dst_reg,
				insn->src_reg, ((u32)insn->imm) >> 16, (u16)insn->imm);
		} else if (is_mov_percpu_addr(insn)) {
@@ -381,7 +381,7 @@ void print_bpf_insn(const struct bpf_insn_cbs *cbs,
				insn->code, class == BPF_JMP32 ? 'w' : 'r',
				insn->dst_reg,
				bpf_jmp_string[BPF_OP(insn->code) >> 4],
				insn->imm, insn->off);
				(u32)insn->imm, insn->off);
		}
	} else {
		verbose(cbs->private_data, "(%02x) %s\n",
+6 −1
Original line number Diff line number Diff line
@@ -71,7 +71,12 @@ prefix ?= /usr/local
bash_compdir ?= /usr/share/bash-completion/completions

CFLAGS += -O2
CFLAGS += -W -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers
CFLAGS += -W
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -Wformat-signedness
CFLAGS += -Wno-unused-parameter
CFLAGS += -Wno-missing-field-initializers
CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS))
CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \
	-I$(or $(OUTPUT),.) \
+7 −7
Original line number Diff line number Diff line
@@ -253,7 +253,7 @@ static int dump_btf_type(const struct btf *btf, __u32 id,
				if (btf_kflag(t))
					printf("\n\t'%s' val=%d", name, v->val);
				else
					printf("\n\t'%s' val=%u", name, v->val);
					printf("\n\t'%s' val=%u", name, (__u32)v->val);
			}
		}
		if (json_output)
@@ -1022,7 +1022,7 @@ static int do_dump(int argc, char **argv)
			for (i = 0; i < root_type_cnt; i++) {
				if (root_type_ids[i] == root_id) {
					err = -EINVAL;
					p_err("duplicate root_id %d supplied", root_id);
					p_err("duplicate root_id %u supplied", root_id);
					goto done;
				}
			}
@@ -1132,7 +1132,7 @@ build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type,
			break;
		default:
			err = -1;
			p_err("unexpected object type: %d", type);
			p_err("unexpected object type: %u", type);
			goto err_free;
		}
		if (err) {
@@ -1155,7 +1155,7 @@ build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type,
			break;
		default:
			err = -1;
			p_err("unexpected object type: %d", type);
			p_err("unexpected object type: %u", type);
			goto err_free;
		}
		if (fd < 0) {
@@ -1188,7 +1188,7 @@ build_btf_type_table(struct hashmap *tab, enum bpf_obj_type type,
			break;
		default:
			err = -1;
			p_err("unexpected object type: %d", type);
			p_err("unexpected object type: %u", type);
			goto err_free;
		}
		if (!btf_id)
@@ -1254,12 +1254,12 @@ show_btf_plain(struct bpf_btf_info *info, int fd,

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

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

	emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
+1 −1
Original line number Diff line number Diff line
@@ -653,7 +653,7 @@ static int __btf_dumper_type_only(const struct btf *btf, __u32 type_id,
	case BTF_KIND_ARRAY:
		array = (struct btf_array *)(t + 1);
		BTF_PRINT_TYPE(array->type);
		BTF_PRINT_ARG("[%d]", array->nelems);
		BTF_PRINT_ARG("[%u]", array->nelems);
		break;
	case BTF_KIND_PTR:
		BTF_PRINT_TYPE(t->type);
+1 −1
Original line number Diff line number Diff line
@@ -191,7 +191,7 @@ static int show_bpf_prog(int id, enum bpf_attach_type attach_type,
		if (attach_btf_name)
			printf(" %-15s", attach_btf_name);
		else if (info.attach_btf_id)
			printf(" attach_btf_obj_id=%d attach_btf_id=%d",
			printf(" attach_btf_obj_id=%u attach_btf_id=%u",
			       info.attach_btf_obj_id, info.attach_btf_id);
		printf("\n");
	}
Loading