diff --git a/ecmascript/base/json_parser.h b/ecmascript/base/json_parser.h index ce921b1016..506a50f984 100644 --- a/ecmascript/base/json_parser.h +++ b/ecmascript/base/json_parser.h @@ -153,6 +153,9 @@ protected: while (true) { SkipStartWhiteSpace(); Tokens token = ParseToken(); + if (current_ > range_) { + THROW_SYNTAX_ERROR_AND_RETURN(thread_, "Unexpected end in JSON", JSTaggedValue::Exception()); + } switch (token) { case Tokens::OBJECT: if (EmptyObjectCheck()) { diff --git a/test/moduletest/jsonparser/expect_output.txt b/test/moduletest/jsonparser/expect_output.txt index 164455a745..8a9130ef75 100644 --- a/test/moduletest/jsonparser/expect_output.txt +++ b/test/moduletest/jsonparser/expect_output.txt @@ -35,3 +35,6 @@ Uf "" Uf 啕晦 +SyntaxError +SyntaxError +SyntaxError diff --git a/test/moduletest/jsonparser/jsonparser.js b/test/moduletest/jsonparser/jsonparser.js index 2277272eca..182821ba6d 100644 --- a/test/moduletest/jsonparser/jsonparser.js +++ b/test/moduletest/jsonparser/jsonparser.js @@ -83,3 +83,27 @@ print(JSON.parse('"\\u5555\\u6666"')); // utf8 -> utf16 print(JSON.parse('["\\"\\"","中文"]')[0]); // utf16 -> utf8 print(JSON.parse('["\\u0055\\u0066","中文"]')[0]); // utf16 -> utf8 print(JSON.parse('["\\u5555\\u6666","中文"]')[0]); // utf16 -> utf16 + +const strData9 = `{"k1":"hello","k2":3}`; +const strErr = strData9.substring(0, strData9.length - 2); +try { + JSON.parse(strErr); +} catch (err) { + print(err.name) +} + +const strData10 = `{"k1":"hello","k2": 3}`; +const strErr2 = strData10.substring(0, strData10.length - 2); +try { + JSON.parse(strErr2); +} catch (err) { + print(err.name) +} + +const strData11 = `{"k1":"hello","k2":311111}`; +const strErr3 = strData11.substring(0, strData11.length - 2); +try { + JSON.parse(strErr3); +} catch (err) { + print(err.name) +} \ No newline at end of file