summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorQuentin Monnet <quentin.monnet@netronome.com>2017-11-29 04:44:28 +0300
committerDaniel Borkmann <daniel@iogearbox.net>2017-11-30 04:09:29 +0300
commit9b85c2d4508563f4bb1de0d971ed02fea0d0d757 (patch)
treee9d2683c58114c5b9c78a058d881c117bf1fd75d /tools
parenta39e17b2d842938e19997d2fdc0443fdd4cd8d10 (diff)
downloadlinux-9b85c2d4508563f4bb1de0d971ed02fea0d0d757.tar.xz
tools: bpftool: fix crash on bad parameters with JSON
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: 004b45c0e51a ("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>
Diffstat (limited to 'tools')
-rw-r--r--tools/bpf/bpftool/main.c19
1 files changed, 9 insertions, 10 deletions
diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c
index d6e4762170a4..14ad54a1c404 100644
--- a/tools/bpf/bpftool/main.c
+++ b/tools/bpf/bpftool/main.c
@@ -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;
@@ -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);