Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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: aff52e685eb3 ("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

authored by

Quentin Monnet and committed by
Andrii Nakryiko
e89ef634 59f2a29c

+9 -7
+9 -7
tools/bpf/bpftool/prog.c
··· 307 307 if (printed_header) 308 308 jsonw_end_object(json_wtr); 309 309 } else { 310 - json_writer_t *btf_wtr = jsonw_new(stdout); 310 + json_writer_t *btf_wtr; 311 311 struct btf_dumper d = { 312 312 .btf = btf, 313 - .jw = btf_wtr, 314 313 .is_plain_text = true, 315 314 }; 316 - 317 - if (!btf_wtr) { 318 - p_err("jsonw alloc failed"); 319 - goto out_free; 320 - } 321 315 322 316 for (i = 0; i < vlen; i++, vsi++) { 323 317 t_var = btf__type_by_id(btf, vsi->type); ··· 322 328 323 329 if (!printed_header) { 324 330 printf("\tmetadata:"); 331 + 332 + btf_wtr = jsonw_new(stdout); 333 + if (!btf_wtr) { 334 + p_err("jsonw alloc failed"); 335 + goto out_free; 336 + } 337 + d.jw = btf_wtr, 338 + 325 339 printed_header = true; 326 340 } 327 341