summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Wadman <mattias.wadman@gmail.com>2023-07-05 18:26:06 +0200
committerNico Williams <nico@cryptonector.com>2023-07-05 16:30:14 -0500
commit95625c3fdb6eec8bcff8c52b93359db3337ba5f9 (patch)
treee432204a48975ef933b30de407374f635b2254f5
parentc077b95ba2dcaafee39e302cc086bf99fa9248d0 (diff)
Validate JSON for --jsonarg
Fixes #2572
-rw-r--r--src/main.c14
-rwxr-xr-xtests/shtest13
2 files changed, 25 insertions, 2 deletions
diff --git a/src/main.c b/src/main.c
index 45f61d0b..6d659110 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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++;
@@ -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++;
diff --git a/tests/shtest b/tests/shtest
index 4cffb1f8..84aa69e8 100755
--- a/tests/shtest
+++ b/tests/shtest
@@ -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