Skip to content

Return error for string literal beginning with single quote #2964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions foo.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flase
6 changes: 6 additions & 0 deletions src/jv_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,12 @@ 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 '/':
case '*':
case '&':
return "Invalid literal";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In @wader's comment above, he found other character's that were triggering the invalid numeric literal. This check catches those. Would this cause an issue?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should it say "Invalid literal" instead of "Invalid numeric literal" for those three characters? How is that better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make more sense for those to trigger Invalid numeric literal? Personally those don't seem like numeric characters (as compared to, say, - which if alone, could be an invalid negative number). I was just implementing the suggestions above. I'm happy to revert if this doesn't make sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Invalid numeric literal" means that jq tried to parse a JSON number and it was not a number. so the error makes sense, yes.
"Invalid literal"? what does that even mean? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. I guess if we got to this point, then we've tried parsing it as a string, boolean, and null. So all that's left is parsing it as a number, hence that message. Maybe the better error message would be something like Invalid character '&' encountered while parsing number? I'm happy to revert this change for now though if everything else looks good.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that ppl search and find #501 (it's the most visited issue) because we mention numeric in many error cases and it's confusing

Had a go at compring jq to JSON.parse:

$ for i in ident truea falsea nana aa1 1aa + - / "&" "*" '(' "'aa'"; do echo "$i:\t$(jq 2>&1 <<< "$i")"; echo "        $(node -p "JSON.parse(\"$i\")" 2>&1 | grep Error | head -n1)"; done
ident:	jq: parse error: Invalid numeric literal at line 2, column 0
        SyntaxError: Unexpected token 'i', "ident" is not valid JSON
truea:	jq: parse error: Invalid literal at line 2, column 0
        SyntaxError: Unexpected non-whitespace character after JSON at position 4 (line 1 column 5)
falsea:	jq: parse error: Invalid literal at line 2, column 0
        SyntaxError: Unexpected non-whitespace character after JSON at position 5 (line 1 column 6)
nana:	jq: parse error: Invalid literal at line 2, column 0
        SyntaxError: Unexpected token 'a', "nana" is not valid JSON
aa1:	jq: parse error: Invalid numeric literal at line 2, column 0
        SyntaxError: Unexpected token 'a', "aa1" is not valid JSON
1aa:	jq: parse error: Invalid numeric literal at line 2, column 0
        SyntaxError: Unexpected non-whitespace character after JSON at position 1 (line 1 column 2)
+:	jq: parse error: Invalid numeric literal at line 2, column 0
        SyntaxError: Unexpected token '+', "+" is not valid JSON
-:	jq: parse error: Invalid numeric literal at line 2, column 0
        SyntaxError: No number after minus sign in JSON at position 1 (line 1 column 2)
/:	jq: parse error: Invalid numeric literal at line 2, column 0
        SyntaxError: Unexpected token '/', "/" is not valid JSON
&:	jq: parse error: Invalid numeric literal at line 2, column 0
        SyntaxError: Unexpected token '&', "&" is not valid JSON
*:	jq: parse error: Invalid numeric literal at line 2, column 0
        SyntaxError: Unexpected token '*', "*" is not valid JSON
(:	jq: parse error: Invalid numeric literal at line 2, column 0
        SyntaxError: Unexpected token '(', "(" is not valid JSON
'aa':	jq: parse error: Invalid numeric literal at line 2, column 0
        SyntaxError: Unexpected token ''', "'aa'" is not valid JSON

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it's ok changing the default error to Invalid character X instead of Invalid numeric literal. It is not ok however to make &, / and * randomly error with "Invalid literal"; while most other character would error e.g. boo, =, ! return Invalid literal.
Anyhow, I only just noticed that Invalid literal is an error that jq already uses instead of Invalid numeric literal for f, n, t because they are short circuited, so that should be changed to be more consistent I guess.
I am fine with the ' patch as a temporary fix for now, but speecial casing &, /, * feels too random.

Should we just change the default error to Invalid character instead, and not even special case ' since Invalid character "'" at is probably good enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happy to implement a general Invalid character "X" kind of error message, but I may need someone to point me in the right direction. My understanding is that the check_literal function returns a const char* meaning we wouldn't be able to get that dynamic error message here without changing a bunch of code. If this change is beyond the scope of the issue, I can just revert back to the simple ' special case.

case 'n':
// if it starts with 'n', it could be a literal "nan"
if (p->tokenpos != 3) {
Expand Down
16 changes: 16 additions & 0 deletions tests/jq.test
Original file line number Diff line number Diff line change
Expand Up @@ -2095,3 +2095,19 @@ null
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}')"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why column 5, not column 2?


try fromjson catch .
"{\"a\": /}"
"Invalid literal at line 1, column 8 (while parsing '{\"a\": /}')"

try fromjson catch .
"{\"a\": *}"
"Invalid literal at line 1, column 8 (while parsing '{\"a\": *}')"

try fromjson catch .
"{\"a\": &}"
"Invalid literal at line 1, column 8 (while parsing '{\"a\": &}')"