Skip to contentSkip to conversation

bpftool: avoid sign extension in unsigned enum JSON dumps

PATCH Applied Hungyu Lin 2 participants 2 messages Oct 30

Subsystems (2)
lore
Share & tools
lore

Hungyu Lin

author
#0 lore raw

PATCH v2 1/1 bpftool: avoid sign extension in unsigned enum JSON dumps

The BTF enum value is stored in a signed 32-bit field even when the enum is unsigned. Passing it directly to jsonw_uint_field(), which takes a uint64_t, sign-extends values with bit 31 set. For example, 0xffffffffU is printed as 18446744073709551615 instead of 4294967295 by bpftool -j btf dump.

Cast the value to __u32 before widening it, matching the conversion already used for plain-text output.

Fixes c93cc69004df ("bpftool: add ability to dump BTF types")

---
Changes in v2:
- Drop RFC as requested by Andrii.
- Add Quentin Monnet's Acked-by.
- No code changes.

v1 (RFC): https://patchwork.kernel.org/series/1196994/

 tools/bpf/bpftool/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/btf.c +1 -1
index abd661db91e6..385521e6b028 100644
@@ -482,7 +482,7 @@ static int dump_btf_type(const struct btf *btf, __u32 id,
 				if (btf_kflag(t))
 					jsonw_int_field(w, "val", v->val);
 				else
-					jsonw_uint_field(w, "val", v->val);
+					jsonw_uint_field(w, "val", (__u32)v->val);
 				jsonw_end_object(w);
 			} else {
 				if (btf_kflag(t))

base-commit: 7101b10cf281f629ac58c99697ca6f6d082b99a7

Signature · 1 line
2.43.0
#1 lore raw
This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 10 lines · On Fri, 30 Oct 2026 19:53:43 +0000 you wrote:

The BTF enum value is stored in a signed 32-bit field even when the enum is unsigned. Passing it directly to jsonw_uint_field(), which takes a uint64_t, sign-extends values with bit 31 set. For example, 0xffffffffU is printed as 18446744073709551615 instead of 4294967295 by bpftool -j btf dump.

Cast the value to __u32 before widening it, matching the conversion already used for plain-text output.

[...]

Here is the summary with links:
- [bpf-next,v2] bpftool: avoid sign extension in unsigned enum JSON dumps

    https://git.kernel.org/bpf/bpf-next/c/eaae576c34d7

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html