Skip to content

Commit

Permalink
Validate JSON for --jsonarg
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Jul 5, 2023
1 parent c077b95 commit 698cc3a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,12 @@ int main(int argc, char* argv[]) {
if (further_args_are_strings) {
ARGS = jv_array_append(ARGS, jv_string(argv[i]));
} else if (further_args_are_json) {
ARGS = jv_array_append(ARGS, jv_parse(argv[i]));
jv v = jv_parse(argv[i]);
if (!jv_is_valid(v)) {
fprintf(stderr, "%s: invalid JSON text passed to --jsonargs\n", progname);
die();
}
ARGS = jv_array_append(ARGS, v);
} else {
jq_util_input_add_input(input_state, argv[i]);
nfiles++;
Expand All @@ -330,7 +335,12 @@ int main(int argc, char* argv[]) {
if (further_args_are_strings) {
ARGS = jv_array_append(ARGS, jv_string(argv[i]));
} else if (further_args_are_json) {
ARGS = jv_array_append(ARGS, jv_parse(argv[i]));
jv v = jv_parse(argv[i]);
if (!jv_is_valid(v)) {
fprintf(stderr, "%s: invalid JSON text passed to --jsonargs\n", progname);
die();
}
ARGS = jv_array_append(ARGS, v);
} else {
jq_util_input_add_input(input_state, argv[i]);
nfiles++;
Expand Down
15 changes: 15 additions & 0 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ fi
echo '{"a":1,"b",' | $JQ --stream > /dev/null 2> $d/err
grep 'Objects must consist of key:value pairs' $d/err > /dev/null

## Regression test for issue #2572 assert when using --jsonargs and invalud JSON
if $JQ -n --jsonargs null invalid; then
: # ok
elif [ $? -ne 2 ]; then
echo "--jsonargs exited with wrong exit code" 1>&2
exit 1
fi
# this tests the args_done code path "--"
if $JQ -n --jsonargs null -- invalid; then
: # ok
elif [ $? -ne 2 ]; then
echo "--jsonargs exited with wrong exit code" 1>&2
exit 1
fi

## Fuzz parser

## XXX With a $(urandom) builtin we could move this test into tests/all.test
Expand Down

0 comments on commit 698cc3a

Please # to comment.