AK: Do not coerce i64 and u64 values to i32 and u32

First, this isn't actually helpful, as we no longer store 32-bit values
in JsonValue. They are stored as 64-bit values anyways.

But more imporatantly, there was a bug here when trying to coerce an i64
to an i32. All negative values were cast to an i32, without checking if
the value is below NumericLimits<i32>::min.
This commit is contained in:
Timothy Flynn 2024-09-26 16:13:24 -04:00 committed by Tim Ledbetter
commit 7b3b608caf
Notes: github-actions[bot] 2024-09-27 08:48:15 +00:00
2 changed files with 32 additions and 13 deletions

View file

@ -128,6 +128,34 @@ TEST_CASE(json_64_bit_value)
EXPECT(big_json_value.equals(big_json_value_copy));
}
TEST_CASE(json_64_bit_value_coerced_to_32_bit)
{
{
auto min = NumericLimits<i64>::min();
auto max = NumericLimits<i64>::max();
auto json = TRY_OR_FAIL(JsonValue::from_string(MUST(String::number(min))));
EXPECT_EQ(json.get_integer<i64>(), min);
EXPECT(!json.is_integer<i32>());
json = TRY_OR_FAIL(JsonValue::from_string(MUST(String::number(max))));
EXPECT_EQ(json.get_integer<i64>(), max);
EXPECT(!json.is_integer<i32>());
}
{
auto min = NumericLimits<u64>::min();
auto max = NumericLimits<u64>::max();
auto json = TRY_OR_FAIL(JsonValue::from_string(MUST(String::number(min))));
EXPECT_EQ(json.get_integer<u64>(), min);
EXPECT_EQ(json.get_integer<u32>(), min);
json = TRY_OR_FAIL(JsonValue::from_string(MUST(String::number(max))));
EXPECT_EQ(json.get_integer<u64>(), max);
EXPECT(!json.is_integer<u32>());
}
}
TEST_CASE(json_duplicate_keys)
{
JsonObject json;