Skip to content

Commit 0b95aaa

Browse files
qmonnetgregkh
authored andcommitted
bpftool: Avoid leaking the JSON writer prepared for program metadata
[ Upstream commit e89ef63 ] 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 Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 7cd4af9 commit 0b95aaa

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

‎tools/bpf/bpftool/prog.c‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -304,18 +304,12 @@ static void show_prog_metadata(int fd, __u32 num_maps)
304304
if (printed_header)
305305
jsonw_end_object(json_wtr);
306306
} else {
307-
json_writer_t *btf_wtr = jsonw_new(stdout);
307+
json_writer_t *btf_wtr;
308308
struct btf_dumper d = {
309309
.btf = btf,
310-
.jw = btf_wtr,
311310
.is_plain_text = true,
312311
};
313312

314-
if (!btf_wtr) {
315-
p_err("jsonw alloc failed");
316-
goto out_free;
317-
}
318-
319313
for (i = 0; i < vlen; i++, vsi++) {
320314
t_var = btf__type_by_id(btf, vsi->type);
321315
name = btf__name_by_offset(btf, t_var->name_off);
@@ -325,6 +319,14 @@ static void show_prog_metadata(int fd, __u32 num_maps)
325319

326320
if (!printed_header) {
327321
printf("\tmetadata:");
322+
323+
btf_wtr = jsonw_new(stdout);
324+
if (!btf_wtr) {
325+
p_err("jsonw alloc failed");
326+
goto out_free;
327+
}
328+
d.jw = btf_wtr,
329+
328330
printed_header = true;
329331
}
330332

0 commit comments

Comments
 (0)