Commit e89ef634 authored by Quentin Monnet's avatar Quentin Monnet Committed by Andrii Nakryiko

bpftool: Avoid leaking the JSON writer prepared for program metadata

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: aff52e68 ("bpftool: Support dumping metadata")
Signed-off-by: default avatarQuentin Monnet <quentin@isovalent.com>
Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20211022094743.11052-1-quentin@isovalent.com
parent 59f2a29c
...@@ -307,18 +307,12 @@ static void show_prog_metadata(int fd, __u32 num_maps) ...@@ -307,18 +307,12 @@ static void show_prog_metadata(int fd, __u32 num_maps)
if (printed_header) if (printed_header)
jsonw_end_object(json_wtr); jsonw_end_object(json_wtr);
} else { } else {
json_writer_t *btf_wtr = jsonw_new(stdout); json_writer_t *btf_wtr;
struct btf_dumper d = { struct btf_dumper d = {
.btf = btf, .btf = btf,
.jw = btf_wtr,
.is_plain_text = true, .is_plain_text = true,
}; };
if (!btf_wtr) {
p_err("jsonw alloc failed");
goto out_free;
}
for (i = 0; i < vlen; i++, vsi++) { for (i = 0; i < vlen; i++, vsi++) {
t_var = btf__type_by_id(btf, vsi->type); t_var = btf__type_by_id(btf, vsi->type);
name = btf__name_by_offset(btf, t_var->name_off); name = btf__name_by_offset(btf, t_var->name_off);
...@@ -328,6 +322,14 @@ static void show_prog_metadata(int fd, __u32 num_maps) ...@@ -328,6 +322,14 @@ static void show_prog_metadata(int fd, __u32 num_maps)
if (!printed_header) { if (!printed_header) {
printf("\tmetadata:"); 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; printed_header = true;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment