Skip to content

Commit 719aec2

Browse files
committed
Validate JSON for --jsonarg
Fixes #2572
1 parent c077b95 commit 719aec2

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/main.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,12 @@ int main(int argc, char* argv[]) {
317317
if (further_args_are_strings) {
318318
ARGS = jv_array_append(ARGS, jv_string(argv[i]));
319319
} else if (further_args_are_json) {
320-
ARGS = jv_array_append(ARGS, jv_parse(argv[i]));
320+
jv v = jv_parse(argv[i]);
321+
if (!jv_is_valid(v)) {
322+
fprintf(stderr, "%s: invalid JSON text passed to --jsonargs\n", progname);
323+
die();
324+
}
325+
ARGS = jv_array_append(ARGS, v);
321326
} else {
322327
jq_util_input_add_input(input_state, argv[i]);
323328
nfiles++;
@@ -330,7 +335,12 @@ int main(int argc, char* argv[]) {
330335
if (further_args_are_strings) {
331336
ARGS = jv_array_append(ARGS, jv_string(argv[i]));
332337
} else if (further_args_are_json) {
333-
ARGS = jv_array_append(ARGS, jv_parse(argv[i]));
338+
jv v = jv_parse(argv[i]);
339+
if (!jv_is_valid(v)) {
340+
fprintf(stderr, "%s: invalid JSON text passed to --jsonargs\n", progname);
341+
die();
342+
}
343+
ARGS = jv_array_append(ARGS, v);
334344
} else {
335345
jq_util_input_add_input(input_state, argv[i]);
336346
nfiles++;

tests/shtest

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,19 @@ fi
207207
echo '{"a":1,"b",' | $JQ --stream > /dev/null 2> $d/err
208208
grep 'Objects must consist of key:value pairs' $d/err > /dev/null
209209

210+
## Regression test for issue #2572 assert when using --jsonargs and invalid JSON
211+
$JQ -n --jsonargs null invalid && EC=$? || EC=$?
212+
if [ "$EC" -ne 2 ]; then
213+
echo "--jsonargs exited with wrong exit code, expected 2 got $EC" 1>&2
214+
exit 1
215+
fi
216+
# this tests the args_done code path "--"
217+
$JQ -n --jsonargs null -- invalid && EC=$? || EC=$?
218+
if [ "$EC" -ne 2 ]; then
219+
echo "--jsonargs exited with wrong exit code, expected 2 got $EC" 1>&2
220+
exit 1
221+
fi
222+
210223
## Fuzz parser
211224

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

0 commit comments

Comments
 (0)