Description: Add endOfString check for JSON parse

Issue: https://gitee.com/openharmony/arkcompiler_ets_runtime/issues/IACTDY?from=project-issue

Signed-off-by: chenjx-huawei <chenjingxiang1@huawei.com>
Change-Id: I407b26534cdd223c60b4fc6ec8b4ae77d2c2c713
This commit is contained in:
chenjx-huawei 2024-07-14 14:41:42 +08:00
parent 41e67820ae
commit 2728135051
3 changed files with 30 additions and 0 deletions

View File

@ -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()) {

View File

@ -35,3 +35,6 @@ Uf
""
Uf
啕晦
SyntaxError
SyntaxError
SyntaxError

View File

@ -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)
}