libbpf: Fix misaligned array closing bracket
authorQuentin Deslandes <qde@naccy.de>
Sat, 13 Apr 2024 21:12:57 +0000 (23:12 +0200)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 17 Apr 2024 13:23:44 +0000 (15:23 +0200)
commit9213e52970a5997c9eb176c7afcc6ec67b1b1e6f
tree5df119d0d36dee376c9b85bc0791b43fc44bc3db
parentad2d22b617b7c0ca2cff4da6dc063183822484bb
libbpf: Fix misaligned array closing bracket

In btf_dump_array_data(), libbpf will call btf_dump_dump_type_data() for
each element. For an array of characters, each element will be
processed the following way:

- btf_dump_dump_type_data() is called to print the character
- btf_dump_data_pfx() prefixes the current line with the proper number
  of indentations
- btf_dump_int_data() is called to print the character
- After the last character is printed, btf_dump_dump_type_data() calls
  btf_dump_data_pfx() before writing the closing bracket

However, for an array containing characters, btf_dump_int_data() won't
print any '\0' and subsequent characters. This leads to situations where
the line prefix is written, no character is added, then the prefix is
written again before adding the closing bracket:

(struct sk_metadata){
    .str_array = (__u8[14])[
        'H',
        'e',
        'l',
        'l',
        'o',
                ],

This change solves this issue by printing the '\0' character, which
has two benefits:

- The bracket closing the array is properly aligned
- It's clear from a user point of view that libbpf uses '\0' as a
  terminator for arrays of characters.

Signed-off-by: Quentin Deslandes <qde@naccy.de>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20240413211258.134421-2-qde@naccy.de
tools/lib/bpf/btf_dump.c