Make stream parser error when a non-empty object or array is used as key

Fixes #2463
This commit is contained in:
Emanuele Torre
2023-07-17 13:15:55 -05:00
committed by Nico Williams
parent e79335e3a5
commit 4b3090a9ab
2 changed files with 32 additions and 0 deletions
+22
View File
@@ -246,6 +246,17 @@ static pfunc stream_token(struct jv_parser* p, char ch) {
case '[':
if (jv_is_valid(p->next))
return "Expected a separator between values";
if (p->last_seen == JV_LAST_OPEN_OBJECT)
// Looks like {["foo"]}
return "Expected string key after '{', not '['";
if (p->last_seen == JV_LAST_COMMA) {
last = jv_array_get(jv_copy(p->path), p->stacklen - 1);
k = jv_get_kind(last);
jv_free(last);
if (k != JV_KIND_NUMBER)
// Looks like {"x":"y",["foo"]}
return "Expected string key after ',' in object, not '['";
}
p->path = jv_array_append(p->path, jv_number(0)); // push
p->last_seen = JV_LAST_OPEN_ARRAY;
p->stacklen++;
@@ -254,6 +265,17 @@ static pfunc stream_token(struct jv_parser* p, char ch) {
case '{':
if (p->last_seen == JV_LAST_VALUE)
return "Expected a separator between values";
if (p->last_seen == JV_LAST_OPEN_OBJECT)
// Looks like {{"foo":"bar"}}
return "Expected string key after '{', not '{'";
if (p->last_seen == JV_LAST_COMMA) {
last = jv_array_get(jv_copy(p->path), p->stacklen - 1);
k = jv_get_kind(last);
jv_free(last);
if (k != JV_KIND_NUMBER)
// Looks like {"x":"y",{"foo":"bar"}}
return "Expected string key after ',' in object, not '{'";
}
// Push object key: null, since we don't know it yet
p->path = jv_array_append(p->path, jv_null()); // push
p->last_seen = JV_LAST_OPEN_OBJECT;
+10
View File
@@ -222,6 +222,16 @@ fi
echo '{"a":1,"b",' | $JQ --stream > /dev/null 2> $d/err || true
grep 'Objects must consist of key:value pairs' $d/err > /dev/null
## Regression tests for issue #2463 assert when stream parse non-scalar object key
echo '{{"a":"b"}}' | $JQ --stream > /dev/null 2> $d/err || true
grep "Expected string key after '{', not '{'" $d/err > /dev/null
echo '{"x":"y",{"a":"b"}}' | $JQ --stream > /dev/null 2> $d/err || true
grep "Expected string key after ',' in object, not '{'" $d/err > /dev/null
echo '{["a","b"]}' | $JQ --stream > /dev/null 2> $d/err || true
grep "Expected string key after '{', not '\\['" $d/err > /dev/null
echo '{"x":"y",["a","b"]}' | $JQ --stream > /dev/null 2> $d/err || true
grep "Expected string key after ',' in object, not '\\['" $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