Skip to content
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

JSON.parse returns evaluation errors instead of throwing them #3392

Open
nicolo-ribaudo opened this issue Aug 14, 2024 · 3 comments · May be fixed by #3394
Open

JSON.parse returns evaluation errors instead of throwing them #3392

nicolo-ribaudo opened this issue Aug 14, 2024 · 3 comments · May be fixed by #3394

Comments

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Aug 14, 2024

Given a variable let input = "[" + "0,".repeat(2**32 - 1) + "0]", this code:

JSON.parse(input)

is defined to return a RangeError (for the invalid array length, as defined in steps 3-5 of https://tc39.es/ecma262/#sec-arraysetlength), rather than throw it.

This is because instep 7-9 of https://tc39.es/ecma262/#sec-json.parse we always get the completion value without actually checking if it's a normal completion or a throw completion.

This is not actually observable in the implementations I could try, since all of them don't allow creating strings that long.

@jmdyck
Copy link
Collaborator

jmdyck commented Aug 14, 2024

Note that if a spec algorithm says to (e.g.) "throw a SyntaxError", that's just a shorthand for returning a particular throw completion. (See 5.2.3.2 Throw an Exception.) There's no semantic difference between returning a throw completion via 'return' (or '?') vs returning it via 'throw'.

@nicolo-ribaudo
Copy link
Member Author

nicolo-ribaudo commented Aug 14, 2024

Ok well, let me reword :)

"When Evaluate returns a throw completion, JSON.parse returns a normal completion containing the throw completion's value"

@jmdyck
Copy link
Collaborator

jmdyck commented Aug 14, 2024

Hm. It seems like step 2 is meant to ensure that step 7's completion is normal. But ECMA-404 doesn't limit the length of an array structure, so your example isn't caught there.

And step 6 asserts that the ParseText in step 4 has no errors. But ArrayLiteral doesn't have an EarlyError if it contains (recursively) too many elements, so again your example passes through.

So yeah, it seems like JSON.parse needs to better handle the possibility that step 7's completion is abrupt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants