1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 23:17:45 +00:00

LibJS: Handle invalid and symbol PropertyName in its AK::Formatter

This would previously crash as we used to_string() without checking the
type first. Circumvent that by handling invalid and numeric ones
separately and then using to_string_or_symbol().
This commit is contained in:
Linus Groh 2021-07-04 17:59:07 +01:00
parent c81001f920
commit 777a93918f

View file

@ -230,9 +230,14 @@ namespace AK {
template<>
struct Formatter<JS::PropertyName> : Formatter<StringView> {
void format(FormatBuilder& builder, JS::PropertyName const& value)
void format(FormatBuilder& builder, JS::PropertyName const& property_name)
{
Formatter<StringView>::format(builder, value.to_string());
if (!property_name.is_valid())
Formatter<StringView>::format(builder, "<invalid PropertyName>");
else if (property_name.is_number())
Formatter<StringView>::format(builder, String::number(property_name.as_number()));
else
Formatter<StringView>::format(builder, property_name.to_string_or_symbol().to_display_string());
}
};