Skip to content

Commit

Permalink
tools: bpftool: fix crash on bad parameters with JSON
Browse files Browse the repository at this point in the history
If bad or unrecognised parameters are specified after JSON output is
requested, `usage()` will try to output null JSON object before the
writer is created.

To prevent this, create the writer as soon as the `--json` option is
parsed.

Fixes: 004b45c ("tools: bpftool: provide JSON output for all possible commands")
Reported-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
  • Loading branch information
Quentin Monnet authored and Daniel Borkmann committed Nov 30, 2017
1 parent a39e17b commit 9b85c2d
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions tools/bpf/bpftool/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,15 @@ int main(int argc, char **argv)
pretty_output = true;
/* fall through */
case 'j':
json_output = true;
if (!json_output) {
json_wtr = jsonw_new(stdout);
if (!json_wtr) {
p_err("failed to create JSON writer");
return -1;
}
json_output = true;
}
jsonw_pretty(json_wtr, pretty_output);
break;
case 'f':
show_pinned = true;
Expand All @@ -306,15 +314,6 @@ int main(int argc, char **argv)
if (argc < 0)
usage();

if (json_output) {
json_wtr = jsonw_new(stdout);
if (!json_wtr) {
p_err("failed to create JSON writer");
return -1;
}
jsonw_pretty(json_wtr, pretty_output);
}

bfd_init();

ret = cmd_select(cmds, argc, argv, do_help);
Expand Down

0 comments on commit 9b85c2d

Please sign in to comment.