Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fixes #2463

authored by Emanuele Torre · committed by Nico Williams · parent e79335e · Browse files · 2 files+32 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+32 added−0 removed

  • tests:+10 added−0 removed
  • other:+22 added−0 removed

2 files+32 added−0 removed

modified:src/jv_parse.c+22 added
244 unmodified lines
245 switch (ch) {246 case '[':247 if (jv_is_valid(p->next))248 return "Expected a separator between values";249 if (p->last_seen == JV_LAST_OPEN_OBJECT)250 // Looks like {["foo"]}251 return "Expected string key after '{', not '['";252 if (p->last_seen == JV_LAST_COMMA) {253 last = jv_array_get(jv_copy(p->path), p->stacklen - 1);254 k = jv_get_kind(last);255 jv_free(last);256 if (k != JV_KIND_NUMBER)257 // Looks like {"x":"y",["foo"]}258 return "Expected string key after ',' in object, not '['";259 }260 p->path = jv_array_append(p->path, jv_number(0)); // push261 p->last_seen = JV_LAST_OPEN_ARRAY;262 p->stacklen++;263 break;264265 case '{':266 if (p->last_seen == JV_LAST_VALUE)267 return "Expected a separator between values";268 if (p->last_seen == JV_LAST_OPEN_OBJECT)269 // Looks like {{"foo":"bar"}}270 return "Expected string key after '{', not '{'";271 if (p->last_seen == JV_LAST_COMMA) {272 last = jv_array_get(jv_copy(p->path), p->stacklen - 1);273 k = jv_get_kind(last);274 jv_free(last);275 if (k != JV_KIND_NUMBER)276 // Looks like {"x":"y",{"foo":"bar"}}277 return "Expected string key after ',' in object, not '{'";278 }279 // Push object key: null, since we don't know it yet280 p->path = jv_array_append(p->path, jv_null()); // push281 p->last_seen = JV_LAST_OPEN_OBJECT;282 p->stacklen++;
620 unmodified lines
modified:tests/shtest+10 added
220 unmodified lines
221## Regression test for issue #2378 assert when stream parse broken object pair222echo '{"a":1,"b",' | $JQ --stream > /dev/null 2> $d/err || true223grep 'Objects must consist of key:value pairs' $d/err > /dev/null224225## Regression tests for issue #2463 assert when stream parse non-scalar object key226echo '{{"a":"b"}}' | $JQ --stream > /dev/null 2> $d/err || true227grep "Expected string key after '{', not '{'" $d/err > /dev/null228echo '{"x":"y",{"a":"b"}}' | $JQ --stream > /dev/null 2> $d/err || true229grep "Expected string key after ',' in object, not '{'" $d/err > /dev/null230echo '{["a","b"]}' | $JQ --stream > /dev/null 2> $d/err || true231grep "Expected string key after '{', not '\\['" $d/err > /dev/null232echo '{"x":"y",["a","b"]}' | $JQ --stream > /dev/null 2> $d/err || true233grep "Expected string key after ',' in object, not '\\['" $d/err > /dev/null234235## Regression test for issue #2572 assert when using --jsonargs and invalid JSON236$JQ -n --jsonargs null invalid && EC=$? || EC=$?237if [ "$EC" -ne 2 ]; then238 echo "--jsonargs exited with wrong exit code, expected 2 got $EC" 1>&2
153 unmodified lines