Repository navigation
Conversation
The leading-zero check in JSONLexer::scanNumber looked at the first two
characters of the number, including the minus sign, so it never fired
for a negative number: JSON.parse("-01") returned -1 and
JSON.parse("-00") returned -0. The JSON number grammar (ECMA-404,
RFC 8259) does not allow a leading zero in the integer part, and
JSON.parse("015") already throws.
Skip the minus sign before checking the integer part.
|
Be sure that |
Author
|
Thanks, both still work. The added test covers them: |
lavenzg
requested changes
Nov 13, 2026
| } | ||
| parse("015"); | ||
| //CHECK-NEXT: SyntaxError: JSON Parse error: Unexpected character in number: 1 | ||
| parse("-015"); |
Contributor
There was a problem hiding this comment.
Please also add test for unicode.
| // If there is a leading minus sign, there must be a digit following it. | ||
| if (LLVM_UNLIKELY(!hasChar() || *iter_.cur < '0' || *iter_.cur > '9')) { | ||
| return error("No digits after minus sign in JSON number"); | ||
| } |
Contributor
There was a problem hiding this comment.
This seems simpler.
if (*iter_.cur == '0') {
++iter_.cur;
if (LLVM_UNLIKELY(hasChar() && *iter_.cur >= '0' && *iter_.cur <= '9'))
return errorWithChar(u"Unexpected character in number: ", *iter_.cur);
}As suggested in review, check the first digit of the integer part while scanning instead of slicing the captured number afterwards. Add json.js cases with a non-ASCII character in the input, which makes JSON.parse use the UTF-16 lexer.
Author
|
Thanks, both done in 0c87619:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2321
JSON.parse("-01")returned-1andJSON.parse("-00")returned-0instead of throwing aSyntaxError. The leading-zero check inJSONLexer::scanNumberreadnumRef[0]andnumRef[1].numRefincludes the minus sign, so the check never fired for a negative number. The JSON number grammar does not allow a leading zero in the integer part (ECMA-404, RFC 8259 section 6).JSON.parse("015")already throws, and V8 throws for-01too.The check now looks at the first digit of the integer part, right after the optional minus sign. The error message is the same as for
015(Unexpected character in number: 1). The check is in the same function as the one that b616636 added for a minus sign without digits.Test Plan
test/hermes/json.js:-015,-00and[-01.5]must throw, and-0,-0.5and[-10]must still parse.015,-015,-00,-0,-0.5and-10are also checked with a non-ASCII character in the input, which makesJSON.parseuse the UTF-16 lexer. Onstatic_h@ cf2ddd2 the test fails atjson.js:65(CHECK-NEXT: expected string not found in input, becauseparse("-015")printed nothing). With this change it passes on all three RUN lines (hermes,hermesrunning the HBC file,shermes -exec).test/hermeslit directory with this change: 660 passed, 3 expected failures, 45 unsupported, no unexpected failures.built-ins/JSONandstaging/sm/JSONthroughutils/test_runner.py: 178 passed, 0 failed, both before and after the change.HermesVMRuntimeTests --gtest_filter='*JSON*': 6/6 passed.clang-format20.1.8 reports no changes forJSONLexer.cpp.-015and-00also parse on the UTF-16 path. The branch is onstatic_h@ cf2ddd2; the one newerstatic_hcommit does not touch JSON code.This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.