Skip to content

Commit

Permalink
Return error for string literal beginning with single quote (#2964)
Browse files Browse the repository at this point in the history
In order for a string literal to be valid in JSON, the value must be
surrounded by double quotes.
  • Loading branch information
mattmeyers authored Oct 5, 2024
1 parent 1bebd23 commit 562d5c5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/jv_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,8 @@ static pfunc check_literal(struct jv_parser* p) {
switch (p->tokenbuf[0]) {
case 't': pattern = "true"; plen = 4; v = jv_true(); break;
case 'f': pattern = "false"; plen = 5; v = jv_false(); break;
case '\'':
return "Invalid string literal; expected \", but got '";
case 'n':
// if it starts with 'n', it could be a literal "nan"
if (p->tokenbuf[1] == 'u') {
Expand Down
3 changes: 3 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -2187,6 +2187,9 @@ try ["ok", setpath([1]; 1)] catch ["ko", .]
{"hi":"hello"}
["ko","Cannot index object with number"]

try fromjson catch .
"{'a': 123}"
"Invalid string literal; expected \", but got ' at line 1, column 5 (while parsing '{'a': 123}')"

# ltrimstr/1 rtrimstr/1 don't leak on invalid input #2977

Expand Down

0 comments on commit 562d5c5

Please sign in to comment.