Skip to content

Commit

Permalink
Validate JSON for --jsonarg
Browse files Browse the repository at this point in the history
Fixes #2572
  • Loading branch information
wader committed Jul 5, 2023
1 parent c077b95 commit 719aec2
Show file tree
Hide file tree
Showing 2 changed files with 25 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
13 changes: 13 additions & 0 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ 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 invalid JSON
$JQ -n --jsonargs null invalid && EC=$? || EC=$?
if [ "$EC" -ne 2 ]; then
echo "--jsonargs exited with wrong exit code, expected 2 got $EC" 1>&2
exit 1
fi
# this tests the args_done code path "--"
$JQ -n --jsonargs null -- invalid && EC=$? || EC=$?
if [ "$EC" -ne 2 ]; then
echo "--jsonargs exited with wrong exit code, expected 2 got $EC" 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 719aec2

Please sign in to comment.