mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
AK: Treat empty string as invalid JSON
Previously we would treat the empty string as `null`. This caused JavaScript like this to fail: ```js var object = {}; try { object = JSON.parse(""); } catch {} var array = object.array || []; ``` Since `JSON.parse("")` returned null instead of throwing, it would set `object` to null and then try and use it instead of using the default backup value.
This commit is contained in:
parent
36c3a0fac2
commit
da25ac0d48
Notes:
sideshowbarker
2024-07-17 09:31:35 +09:00
Author: https://github.com/Lubrsi Commit: https://github.com/SerenityOS/serenity/commit/da25ac0d48 Pull-request: https://github.com/SerenityOS/serenity/pull/14549
@ -236,8 +236,6 @@ void JsonValue::clear()
|
|||||||
#ifndef KERNEL
|
#ifndef KERNEL
|
||||||
ErrorOr<JsonValue> JsonValue::from_string(StringView input)
|
ErrorOr<JsonValue> JsonValue::from_string(StringView input)
|
||||||
{
|
{
|
||||||
if (input.is_empty())
|
|
||||||
return JsonValue();
|
|
||||||
return JsonParser(input).parse();
|
return JsonParser(input).parse();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -126,7 +126,7 @@ TEST_CASE(json_u64_roundtrip)
|
|||||||
TEST_CASE(json_parse_empty_string)
|
TEST_CASE(json_parse_empty_string)
|
||||||
{
|
{
|
||||||
auto value = JsonValue::from_string("");
|
auto value = JsonValue::from_string("");
|
||||||
EXPECT_EQ(value.value().is_null(), true);
|
EXPECT_EQ(value.is_error(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE(json_parse_long_decimals)
|
TEST_CASE(json_parse_long_decimals)
|
||||||
|
@ -29,6 +29,7 @@ test("syntax errors", () => {
|
|||||||
"[1,2,3, ]",
|
"[1,2,3, ]",
|
||||||
'{ "foo": "bar",}',
|
'{ "foo": "bar",}',
|
||||||
'{ "foo": "bar", }',
|
'{ "foo": "bar", }',
|
||||||
|
"",
|
||||||
].forEach(test => {
|
].forEach(test => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
JSON.parse(test);
|
JSON.parse(test);
|
||||||
|
Loading…
Reference in New Issue
Block a user