Skip to content

Commit

Permalink
bpftool: Avoid leaking the JSON writer prepared for program metadata
Browse files Browse the repository at this point in the history
Bpftool creates a new JSON object for writing program metadata in plain
text mode, regardless of metadata being present or not. Then this writer
is freed if any metadata has been found and printed, but it leaks
otherwise. We cannot destroy the object unconditionally, because the
destructor prints an undesirable line break. Instead, make sure the
writer is created only after we have found program metadata to print.

Found with valgrind.

Fixes: aff52e6 ("bpftool: Support dumping metadata")
Signed-off-by: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211022094743.11052-1-quentin@isovalent.com
  • Loading branch information
Quentin Monnet authored and Andrii Nakryiko committed Oct 22, 2021
1 parent 59f2a29 commit e89ef63
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions tools/bpf/bpftool/prog.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,12 @@ static void show_prog_metadata(int fd, __u32 num_maps)
if (printed_header)
jsonw_end_object(json_wtr);
} else {
json_writer_t *btf_wtr = jsonw_new(stdout);
json_writer_t *btf_wtr;
struct btf_dumper d = {
.btf = btf,
.jw = btf_wtr,
.is_plain_text = true,
};

if (!btf_wtr) {
p_err("jsonw alloc failed");
goto out_free;
}

for (i = 0; i < vlen; i++, vsi++) {
t_var = btf__type_by_id(btf, vsi->type);
name = btf__name_by_offset(btf, t_var->name_off);
Expand All @@ -328,6 +322,14 @@ static void show_prog_metadata(int fd, __u32 num_maps)

if (!printed_header) {
printf("\tmetadata:");

btf_wtr = jsonw_new(stdout);
if (!btf_wtr) {
p_err("jsonw alloc failed");
goto out_free;
}
d.jw = btf_wtr,

printed_header = true;
}

Expand Down

0 comments on commit e89ef63

Please sign in to comment.