From 562d5c52226e27f5b422c83ec0c97ff9ba305399 Mon Sep 17 00:00:00 2001 From: Matt Meyers Date: Sat, 5 Oct 2024 19:52:56 -0400 Subject: [PATCH] Return error for string literal beginning with single quote (#2964) In order for a string literal to be valid in JSON, the value must be surrounded by double quotes. --- src/jv_parse.c | 2 ++ tests/jq.test | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/jv_parse.c b/src/jv_parse.c index 9755b8ac47..519c2047f2 100644 --- a/src/jv_parse.c +++ b/src/jv_parse.c @@ -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') { diff --git a/tests/jq.test b/tests/jq.test index 83d817d4e5..404994e268 100644 --- a/tests/jq.test +++ b/tests/jq.test @@ -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