Skip to content

Commit

Permalink
Make stream parser error when a non-empty object or array is used as key
Browse files Browse the repository at this point in the history
Fixes #2463
  • Loading branch information
emanuele6 authored and nicowilliams committed Jul 17, 2023
1 parent e79335e commit 4b3090a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/jv_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions tests/shtest
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4b3090a

Please sign in to comment.