diff --git a/lib/VM/JSLib/JSONLexer.cpp b/lib/VM/JSLib/JSONLexer.cpp index 66d6b9490b7..51189279679 100644 --- a/lib/VM/JSLib/JSONLexer.cpp +++ b/lib/VM/JSLib/JSONLexer.cpp @@ -195,6 +195,13 @@ ExecutionStatus JSONLexer::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; @@ -221,16 +228,6 @@ ExecutionStatus JSONLexer::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; diff --git a/test/hermes/json.js b/test/hermes/json.js index e5755693761..55b1363534d 100644 --- a/test/hermes/json.js +++ b/test/hermes/json.js @@ -61,6 +61,24 @@ function parse(json) { } parse("015"); //CHECK-NEXT: SyntaxError: JSON Parse error: Unexpected character in number: 1 +parse("-015"); +//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("-");