!8163 Add endOfString check for JSON parse

Merge pull request !8163 from chenjingxiang/opt_jsonparse_slicedstring_br
This commit is contained in:
openharmony_ci 2024-07-14 16:24:07 +00:00 committed by Gitee
commit 9baa81aedc
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
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)
}