Browse Source

LibJS+LibLocale: Propagate OOM from CLDR NumberFormat Vector operations

Timothy Flynn 2 years ago
parent
commit
89da8de4ca

+ 10 - 10
Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateNumberFormatData.cpp

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
+ * Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
  */
@@ -798,7 +798,7 @@ namespace Locale {
 
     generator.append(R"~~~(
 struct NumberFormatImpl {
-    NumberFormat to_unicode_number_format() const {
+    ErrorOr<NumberFormat> to_unicode_number_format() const {
         NumberFormat number_format {};
 
         number_format.magnitude = magnitude;
@@ -808,9 +808,9 @@ struct NumberFormatImpl {
         number_format.positive_format = decode_string(positive_format);
         number_format.negative_format = decode_string(negative_format);
 
-        number_format.identifiers.ensure_capacity(identifiers.size());
+        TRY(number_format.identifiers.try_ensure_capacity(identifiers.size()));
         for (@string_index_type@ identifier : identifiers)
-            number_format.identifiers.append(decode_string(identifier));
+            number_format.identifiers.unchecked_append(decode_string(identifier));
 
         return number_format;
     }
@@ -1017,7 +1017,7 @@ ErrorOr<Optional<NumberFormat>> get_standard_number_system_format(StringView loc
             break;
         }
 
-        return s_number_formats[format_index].to_unicode_number_format();
+        return TRY(s_number_formats[format_index].to_unicode_number_format());
     }
 
     return OptionalNone {};
@@ -1046,10 +1046,10 @@ ErrorOr<Vector<NumberFormat>> get_compact_number_system_formats(StringView local
         }
 
         auto number_formats = s_number_format_lists.at(number_format_list_index);
-        formats.ensure_capacity(number_formats.size());
+        TRY(formats.try_ensure_capacity(number_formats.size()));
 
         for (auto number_format : number_formats)
-            formats.append(s_number_formats[number_format].to_unicode_number_format());
+            formats.unchecked_append(TRY(s_number_formats[number_format].to_unicode_number_format()));
     }
 
     return formats;
@@ -1074,7 +1074,7 @@ static Unit const* find_units(StringView locale, StringView unit)
     return nullptr;
 }
 
-Vector<NumberFormat> get_unit_formats(StringView locale, StringView unit, Style style)
+ErrorOr<Vector<NumberFormat>> get_unit_formats(StringView locale, StringView unit, Style style)
 {
     Vector<NumberFormat> formats;
 
@@ -1096,10 +1096,10 @@ Vector<NumberFormat> get_unit_formats(StringView locale, StringView unit, Style
         }
 
         auto number_formats = s_number_format_lists.at(number_format_list_index);
-        formats.ensure_capacity(number_formats.size());
+        TRY(formats.try_ensure_capacity(number_formats.size()));
 
         for (auto number_format : number_formats)
-            formats.append(s_number_formats[number_format].to_unicode_number_format());
+            formats.unchecked_append(TRY(s_number_formats[number_format].to_unicode_number_format()));
     }
 
     return formats;

+ 1 - 1
Userland/Libraries/LibJS/Runtime/Intl/NumberFormat.cpp

@@ -1317,7 +1317,7 @@ ThrowCompletionOr<Optional<Variant<StringView, String>>> get_number_format_patte
         //     i. Let unit be "fallback".
         // e. Let patterns be patterns.[[<unit>]].
         // f. Let patterns be patterns.[[<unitDisplay>]].
-        auto formats = ::Locale::get_unit_formats(number_format.data_locale(), number_format.unit(), number_format.unit_display());
+        auto formats = TRY_OR_THROW_OOM(vm, ::Locale::get_unit_formats(number_format.data_locale(), number_format.unit(), number_format.unit_display()));
         auto plurality = MUST_OR_THROW_OOM(resolve_plural(vm, number_format, ::Locale::PluralForm::Cardinal, number.to_value(vm)));
 
         if (auto it = formats.find_if([&](auto& p) { return p.plurality == plurality.plural_category; }); it != formats.end())

+ 1 - 1
Userland/Libraries/LibLocale/NumberFormat.cpp

@@ -20,7 +20,7 @@ ErrorOr<Optional<StringView>> __attribute__((weak)) get_number_system_symbol(Str
 ErrorOr<Optional<NumberGroupings>> __attribute__((weak)) get_number_system_groupings(StringView, StringView) { return OptionalNone {}; }
 ErrorOr<Optional<NumberFormat>> __attribute__((weak)) get_standard_number_system_format(StringView, StringView, StandardNumberFormatType) { return OptionalNone {}; }
 ErrorOr<Vector<NumberFormat>> __attribute__((weak)) get_compact_number_system_formats(StringView, StringView, CompactNumberFormatType) { return Vector<NumberFormat> {}; }
-Vector<NumberFormat> __attribute__((weak)) get_unit_formats(StringView, StringView, Style) { return {}; }
+ErrorOr<Vector<NumberFormat>> __attribute__((weak)) get_unit_formats(StringView, StringView, Style) { return Vector<NumberFormat> {}; }
 
 Optional<Span<u32 const>> __attribute__((weak)) get_digits_for_number_system(StringView)
 {

+ 1 - 1
Userland/Libraries/LibLocale/NumberFormat.h

@@ -69,7 +69,7 @@ ErrorOr<String> replace_digits_for_number_system(StringView system, StringView n
 
 ErrorOr<Optional<NumberFormat>> get_standard_number_system_format(StringView locale, StringView system, StandardNumberFormatType type);
 ErrorOr<Vector<NumberFormat>> get_compact_number_system_formats(StringView locale, StringView system, CompactNumberFormatType type);
-Vector<NumberFormat> get_unit_formats(StringView locale, StringView unit, Style style);
+ErrorOr<Vector<NumberFormat>> get_unit_formats(StringView locale, StringView unit, Style style);
 
 ErrorOr<Optional<String>> augment_currency_format_pattern(StringView currency_display, StringView base_pattern);
 ErrorOr<Optional<String>> augment_range_pattern(StringView range_separator, StringView lower, StringView upper);