Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 7 additions & 10 deletions lib/VM/JSLib/JSONLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ ExecutionStatus JSONLexer<Kind>::scanNumber() {
}
}

// The integer part cannot start with 0, unless it's 0.
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);
}

bool isTrivialInteger = true;
uint64_t integer = 0;
constexpr uint64_t kFirstIntThatCanOverflow = (UINT64_MAX - 9) / 10;
Expand All @@ -221,16 +228,6 @@ ExecutionStatus JSONLexer<Kind>::scanNumber() {
numRef = iter_.cur.endCapture();
}

// Check for illegal leading 0.
size_t len = numRef.size();
assert(len > 0 && "scanNumber must be called on a number-looking char");
if (LLVM_UNLIKELY(
numRef[0] == '0' && len > 1 && numRef[1] >= '0' &&
numRef[1] <= '9')) {
// The integer part cannot start with 0, unless it's 0.
return errorWithChar(u"Unexpected character in number: ", numRef[1]);
}

if (isTrivialInteger) {
token_.setNumber((double)integer * multiplier);
return ExecutionStatus::RETURNED;
Expand Down
18 changes: 18 additions & 0 deletions test/hermes/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ function parse(json) {
}
parse("015");
//CHECK-NEXT: SyntaxError: JSON Parse error: Unexpected character in number: 1
parse("-015");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please also add test for unicode.

//CHECK-NEXT: SyntaxError: JSON Parse error: Unexpected character in number: 1
parse("-00");
//CHECK-NEXT: SyntaxError: JSON Parse error: Unexpected character in number: 0
parse("[-01.5]");
//CHECK-NEXT: SyntaxError: JSON Parse error: Unexpected character in number: 1
print(1 / JSON.parse("-0"), JSON.parse("-0.5"), JSON.parse("[-10]")[0]);
//CHECK-NEXT: -Infinity -0.5 -10
// A non-ASCII character in the input makes JSON.parse use the UTF-16 lexer.
parse("[\"\u4f60\", 015]");
//CHECK-NEXT: SyntaxError: JSON Parse error: Unexpected character in number: 1
parse("[\"\u4f60\", -015]");
//CHECK-NEXT: SyntaxError: JSON Parse error: Unexpected character in number: 1
parse("[\"\u4f60\", -00]");
//CHECK-NEXT: SyntaxError: JSON Parse error: Unexpected character in number: 0
var u = JSON.parse("[\"\u4f60\", -0, -0.5, -10]");
print(1 / u[1], u[2], u[3]);
//CHECK-NEXT: -Infinity -0.5 -10
parse("+5");
//CHECK-NEXT: SyntaxError: JSON Parse error: Unexpected character: +
parse("-");
Expand Down
Loading