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

Configure Feed

Select the types of activity you want to include in your feed.

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>

+39 -33
+2 -2
kernel/bpf/disasm.c
··· 202 202 insn->dst_reg, class == BPF_ALU ? 'w' : 'r', 203 203 insn->dst_reg); 204 204 } 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", 206 206 insn->code, insn->dst_reg, 207 207 insn->src_reg, ((u32)insn->imm) >> 16, (u16)insn->imm); 208 208 } else if (is_mov_percpu_addr(insn)) { ··· 381 381 insn->code, class == BPF_JMP32 ? 'w' : 'r', 382 382 insn->dst_reg, 383 383 bpf_jmp_string[BPF_OP(insn->code) >> 4], 384 - insn->imm, insn->off); 384 + (u32)insn->imm, insn->off); 385 385 } 386 386 } else { 387 387 verbose(cbs->private_data, "(%02x) %s\n",
+6 -1
tools/bpf/bpftool/Makefile
··· 71 71 bash_compdir ?= /usr/share/bash-completion/completions 72 72 73 73 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 75 80 CFLAGS += $(filter-out -Wswitch-enum -Wnested-externs,$(EXTRA_WARNINGS)) 76 81 CFLAGS += -DPACKAGE='"bpftool"' -D__EXPORTED_HEADERS__ \ 77 82 -I$(or $(OUTPUT),.) \
+7 -7
tools/bpf/bpftool/btf.c
··· 253 253 if (btf_kflag(t)) 254 254 printf("\n\t'%s' val=%d", name, v->val); 255 255 else 256 - printf("\n\t'%s' val=%u", name, v->val); 256 + printf("\n\t'%s' val=%u", name, (__u32)v->val); 257 257 } 258 258 } 259 259 if (json_output) ··· 1022 1022 for (i = 0; i < root_type_cnt; i++) { 1023 1023 if (root_type_ids[i] == root_id) { 1024 1024 err = -EINVAL; 1025 - p_err("duplicate root_id %d supplied", root_id); 1025 + p_err("duplicate root_id %u supplied", root_id); 1026 1026 goto done; 1027 1027 } 1028 1028 } ··· 1132 1132 break; 1133 1133 default: 1134 1134 err = -1; 1135 - p_err("unexpected object type: %d", type); 1135 + p_err("unexpected object type: %u", type); 1136 1136 goto err_free; 1137 1137 } 1138 1138 if (err) { ··· 1155 1155 break; 1156 1156 default: 1157 1157 err = -1; 1158 - p_err("unexpected object type: %d", type); 1158 + p_err("unexpected object type: %u", type); 1159 1159 goto err_free; 1160 1160 } 1161 1161 if (fd < 0) { ··· 1188 1188 break; 1189 1189 default: 1190 1190 err = -1; 1191 - p_err("unexpected object type: %d", type); 1191 + p_err("unexpected object type: %u", type); 1192 1192 goto err_free; 1193 1193 } 1194 1194 if (!btf_id) ··· 1254 1254 1255 1255 n = 0; 1256 1256 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); 1258 1258 } 1259 1259 1260 1260 n = 0; 1261 1261 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); 1263 1263 } 1264 1264 1265 1265 emit_obj_refs_plain(refs_table, info->id, "\n\tpids ");
+1 -1
tools/bpf/bpftool/btf_dumper.c
··· 653 653 case BTF_KIND_ARRAY: 654 654 array = (struct btf_array *)(t + 1); 655 655 BTF_PRINT_TYPE(array->type); 656 - BTF_PRINT_ARG("[%d]", array->nelems); 656 + BTF_PRINT_ARG("[%u]", array->nelems); 657 657 break; 658 658 case BTF_KIND_PTR: 659 659 BTF_PRINT_TYPE(t->type);
+1 -1
tools/bpf/bpftool/cgroup.c
··· 191 191 if (attach_btf_name) 192 192 printf(" %-15s", attach_btf_name); 193 193 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", 195 195 info.attach_btf_obj_id, info.attach_btf_id); 196 196 printf("\n"); 197 197 }
+2 -2
tools/bpf/bpftool/common.c
··· 714 714 int vendor_id; 715 715 716 716 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, 718 718 strerror(errno)); 719 719 return NULL; 720 720 } ··· 739 739 /* No NFP support in LLVM, we have no valid triple to return. */ 740 740 default: 741 741 p_err("Can't get arch name for device vendor id 0x%04x", 742 - vendor_id); 742 + (unsigned int)vendor_id); 743 743 return NULL; 744 744 } 745 745 }
+2 -1
tools/bpf/bpftool/jit_disasm.c
··· 343 343 { 344 344 const struct bpf_line_info *linfo = NULL; 345 345 unsigned int nr_skip = 0; 346 - int count, i, pc = 0; 346 + int count, i; 347 + unsigned int pc = 0; 347 348 disasm_ctx_t ctx; 348 349 349 350 if (!len)
+3 -3
tools/bpf/bpftool/map_perf_ring.c
··· 91 91 jsonw_end_object(json_wtr); 92 92 } else { 93 93 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", 95 95 e->time / 1000000000ULL, e->time % 1000000000ULL, 96 96 cpu, idx); 97 97 fprint_hex(stdout, e->data, e->size, " "); 98 98 printf("\n"); 99 99 } else if (e->header.type == PERF_RECORD_LOST) { 100 - printf("lost %lld events\n", lost->lost); 100 + printf("lost %llu events\n", lost->lost); 101 101 } else { 102 - printf("unknown event type=%d size=%d\n", 102 + printf("unknown event type=%u size=%u\n", 103 103 e->header.type, e->header.size); 104 104 } 105 105 }
+2 -2
tools/bpf/bpftool/net.c
··· 476 476 for (i = 0; i < optq.count; i++) { 477 477 NET_START_OBJECT; 478 478 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); 480 480 NET_DUMP_STR("kind", " %s", attach_loc_strings[loc]); 481 481 ret = __show_dev_tc_bpf_name(prog_ids[i], prog_name, 482 482 sizeof(prog_name)); ··· 831 831 if (err) { 832 832 if (errno == ENOENT) 833 833 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); 835 835 break; 836 836 } 837 837
+6 -6
tools/bpf/bpftool/prog.c
··· 521 521 print_dev_plain(info->ifindex, info->netns_dev, info->netns_ino); 522 522 printf("%s", info->gpl_compatible ? " gpl" : ""); 523 523 if (info->run_time_ns) 524 - printf(" run_time_ns %lld run_cnt %lld", 524 + printf(" run_time_ns %llu run_cnt %llu", 525 525 info->run_time_ns, info->run_cnt); 526 526 if (info->recursion_misses) 527 - printf(" recursion_misses %lld", info->recursion_misses); 527 + printf(" recursion_misses %llu", info->recursion_misses); 528 528 printf("\n"); 529 529 } 530 530 ··· 569 569 } 570 570 571 571 if (info->btf_id) 572 - printf("\n\tbtf_id %d", info->btf_id); 572 + printf("\n\tbtf_id %u", info->btf_id); 573 573 574 574 emit_obj_refs_plain(refs_table, info->id, "\n\tpids "); 575 575 ··· 1164 1164 } 1165 1165 if (nb_read > buf_size - block_size) { 1166 1166 if (buf_size == UINT32_MAX) { 1167 - p_err("data_in/ctx_in is too long (max: %d)", 1167 + p_err("data_in/ctx_in is too long (max: %u)", 1168 1168 UINT32_MAX); 1169 1169 goto err_free; 1170 1170 } ··· 2252 2252 2253 2253 t = btf__type_by_id(btf, func_info.type_id); 2254 2254 if (!t) { 2255 - p_err("btf %d doesn't have type %d", 2255 + p_err("btf %u doesn't have type %u", 2256 2256 info.btf_id, func_info.type_id); 2257 2257 goto out; 2258 2258 } ··· 2330 2330 continue; 2331 2331 for (cpu = 0; cpu < obj->rodata->num_cpu; cpu++) { 2332 2332 if (profile_open_perf_event(m, cpu, map_fd)) { 2333 - p_err("failed to create event %s on cpu %d", 2333 + p_err("failed to create event %s on cpu %u", 2334 2334 metrics[m].name, cpu); 2335 2335 return -1; 2336 2336 }
+1 -1
tools/bpf/bpftool/tracelog.c
··· 78 78 return false; 79 79 80 80 /* Allow room for NULL terminating byte and pipe file name */ 81 - snprintf(format, sizeof(format), "%%*s %%%zds %%99s %%*s %%*d %%*d\\n", 81 + snprintf(format, sizeof(format), "%%*s %%%zus %%99s %%*s %%*d %%*d\\n", 82 82 PATH_MAX - strlen(pipe_name) - 1); 83 83 while (fscanf(fp, format, mnt, type) == 2) 84 84 if (strcmp(type, fstype) == 0) {
+3 -3
tools/bpf/bpftool/xlated_dumper.c
··· 199 199 200 200 if (insn->src_reg == BPF_PSEUDO_MAP_FD) 201 201 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), 202 - "map[id:%u]", insn->imm); 202 + "map[id:%d]", insn->imm); 203 203 else if (insn->src_reg == BPF_PSEUDO_MAP_VALUE) 204 204 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), 205 - "map[id:%u][0]+%u", insn->imm, (insn + 1)->imm); 205 + "map[id:%d][0]+%d", insn->imm, (insn + 1)->imm); 206 206 else if (insn->src_reg == BPF_PSEUDO_MAP_IDX_VALUE) 207 207 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), 208 - "map[idx:%u]+%u", insn->imm, (insn + 1)->imm); 208 + "map[idx:%d]+%d", insn->imm, (insn + 1)->imm); 209 209 else if (insn->src_reg == BPF_PSEUDO_FUNC) 210 210 snprintf(dd->scratch_buff, sizeof(dd->scratch_buff), 211 211 "subprog[%+d]", insn->imm);