Re: [PATCH bpf-next v1 3/4] bpftool: Don't drop a type in the sorted C dump

Andrii Nakryiko <[email protected]>
Newsgroups org.kernel.vger.bpf
Message-ID <CAEf4Bzb0ybaCSPU9pTKhW3ZEtmWS+o+7MShpDCLysQrULM8aoA@mail.gmail.com>
On Wed, Aug 19, 2026 at 5:06 PM Ihor Solodrai <[email protected]> wrote:
>
> The C dump sorts types by default, so that generated headers are
> diffable. The sorted dump emits one type fewer than the unsorted dump
> of the same BTF.
>
> dump_btf_c() starts its loop at index 1 to skip the void type at BTF
> type ID 0. That holds for the unsorted dump, where the array index is
> the type ID, but not after qsort(): position 0 is then the lowest ranked
> type, and btf_type_rank() ranks an anonymous enum 0 while void takes the
> default rank of 10. So the enum is skipped, and void is emitted instead
> as a no-op.
>
> Skip by type ID rather than by position.
>
> Fixes: 94133cf24bb3 ("bpftool: Introduce btf c dump sorting")
> Signed-off-by: Ihor Solodrai <[email protected]>
> ---
>  tools/bpf/bpftool/btf.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/tools/bpf/bpftool/btf.c b/tools/bpf/bpftool/btf.c
> index c9589026da8d..345d49a9a22c 100644
> --- a/tools/bpf/bpftool/btf.c
> +++ b/tools/bpf/bpftool/btf.c
> @@ -805,9 +805,13 @@ static int dump_btf_c(const struct btf *btf,
>
>                 if (sort_dump)
>                         datums = sort_btf_c(btf);
> -               for (i = 1; i < cnt; i++) {
> +               for (i = 0; i < cnt; i++) {
>                         int idx = datums ? datums[i].index : i;
>
> +                       /* type ID 0 is void, skip it */
> +                       if (!idx)

nit: my personal preference, but when dealing with integers that do
not represent "error/success" (zero being a success), let's not use
boolean operations. this should be obvious `idx == 0`, IMO

pw-bot: cr


(but nice find!)

> +                               continue;
> +
>                         err = btf_dump__dump_type(d, idx);
>                         if (err)
>                                 goto done;
> --
> 2.55.0
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.