AK: Fix JSON parser crashing when encountering UTF-8

The mechanism that caches the most recently seen string for each first
character was indexing into the cache using a 'char' subscript. Oops!
This commit is contained in:
Andreas Kling 2019-12-29 22:18:45 +01:00
parent d1d7db2745
commit 821484f170
Notes: sideshowbarker 2024-07-19 10:33:28 +09:00
2 changed files with 9 additions and 1 deletions

View File

@ -103,7 +103,7 @@ String JsonParser::consume_quoted_string()
if (buffer.is_empty()) if (buffer.is_empty())
return String::empty(); return String::empty();
auto& last_string_starting_with_character = m_last_string_starting_with_character[(int)buffer.first()]; auto& last_string_starting_with_character = m_last_string_starting_with_character[(u8)buffer.first()];
if (last_string_starting_with_character.length() == (size_t)buffer.size()) { if (last_string_starting_with_character.length() == (size_t)buffer.size()) {
if (!memcmp(last_string_starting_with_character.characters(), buffer.data(), buffer.size())) if (!memcmp(last_string_starting_with_character.characters(), buffer.data(), buffer.size()))
return last_string_starting_with_character; return last_string_starting_with_character;

View File

@ -74,4 +74,12 @@ TEST_CASE(json_empty_string)
EXPECT_EQ(json.as_string().is_empty(), true); EXPECT_EQ(json.as_string().is_empty(), true);
} }
TEST_CASE(json_utf8_character)
{
auto json = JsonValue::from_string("\"\xc3\x84\"");
EXPECT_EQ(json.type(), JsonValue::Type::String);
EXPECT_EQ(json.as_string().is_null(), false);
EXPECT_EQ(json.as_string().length(), 2);
}
TEST_MAIN(JSON) TEST_MAIN(JSON)