Skip to content

Commit 340434c

Browse files
trflynn89linusg
authored andcommitted
LibLocale: Propagate OOM from CLDR DateTime Vector and String operations
1 parent 89da8de commit 340434c

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

Meta/Lagom/Tools/CodeGenerators/LibLocale/GenerateDateTimeFormatData.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2022, Tim Flynn <trflynn89@serenityos.org>
2+
* Copyright (c) 2021-2023, Tim Flynn <trflynn89@serenityos.org>
33
*
44
* SPDX-License-Identifier: BSD-2-Clause
55
*/
@@ -2047,19 +2047,19 @@ static Optional<Calendar> keyword_to_calendar(KeywordCalendar keyword)
20472047
}
20482048
}
20492049
2050-
Vector<HourCycle> get_regional_hour_cycles(StringView region)
2050+
ErrorOr<Vector<HourCycle>> get_regional_hour_cycles(StringView region)
20512051
{
20522052
auto region_value = hour_cycle_region_from_string(region);
20532053
if (!region_value.has_value())
2054-
return {};
2054+
return Vector<HourCycle> {};
20552055
20562056
auto region_index = to_underlying(*region_value);
20572057
20582058
auto regional_hour_cycles_index = s_hour_cycles.at(region_index);
20592059
auto const& regional_hour_cycles = s_hour_cycle_lists.at(regional_hour_cycles_index);
20602060
20612061
Vector<HourCycle> hour_cycles;
2062-
hour_cycles.ensure_capacity(regional_hour_cycles.size());
2062+
TRY(hour_cycles.try_ensure_capacity(regional_hour_cycles.size()));
20632063
20642064
for (auto hour_cycle : regional_hour_cycles)
20652065
hour_cycles.unchecked_append(static_cast<HourCycle>(hour_cycle));

Userland/Libraries/LibLocale/DateTimeFormat.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ StringView calendar_pattern_style_to_string(CalendarPatternStyle style)
9292
}
9393

9494
Optional<HourCycleRegion> __attribute__((weak)) hour_cycle_region_from_string(StringView) { return {}; }
95-
Vector<HourCycle> __attribute__((weak)) get_regional_hour_cycles(StringView) { return {}; }
95+
ErrorOr<Vector<HourCycle>> __attribute__((weak)) get_regional_hour_cycles(StringView) { return Vector<HourCycle> {}; }
9696

97-
template<typename T, typename GetRegionalValues>
97+
template<typename T, FallibleFunction<StringView> GetRegionalValues>
9898
static ErrorOr<T> find_regional_values_for_locale(StringView locale, GetRegionalValues&& get_regional_values)
9999
{
100100
auto has_value = [](auto const& container) {
@@ -104,7 +104,7 @@ static ErrorOr<T> find_regional_values_for_locale(StringView locale, GetRegional
104104
return !container.is_empty();
105105
};
106106

107-
if (auto regional_values = get_regional_values(locale); has_value(regional_values))
107+
if (auto regional_values = TRY(get_regional_values(locale)); has_value(regional_values))
108108
return regional_values;
109109

110110
auto return_default_values = [&]() { return get_regional_values("001"sv); };
@@ -118,12 +118,18 @@ static ErrorOr<T> find_regional_values_for_locale(StringView locale, GetRegional
118118
if (!language.has_value() || !language->region.has_value())
119119
return return_default_values();
120120

121-
if (auto regional_values = get_regional_values(*language->region); has_value(regional_values))
121+
if (auto regional_values = TRY(get_regional_values(*language->region)); has_value(regional_values))
122122
return regional_values;
123123

124124
return return_default_values();
125125
}
126126

127+
template<typename T, typename GetRegionalValues>
128+
static ErrorOr<T> find_regional_values_for_locale(StringView locale, GetRegionalValues&& get_regional_values)
129+
{
130+
return find_regional_values_for_locale<T>(locale, [&](auto region) -> ErrorOr<T> { return get_regional_values(region); });
131+
}
132+
127133
// https://unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table
128134
ErrorOr<Vector<HourCycle>> get_locale_hour_cycles(StringView locale)
129135
{
@@ -187,22 +193,22 @@ ErrorOr<String> combine_skeletons(StringView first, StringView second)
187193

188194
StringBuilder builder;
189195

190-
auto append_from_skeleton = [&](auto skeleton, auto ch) {
196+
auto append_from_skeleton = [&](auto skeleton, auto ch) -> ErrorOr<bool> {
191197
auto first_index = skeleton.find(ch);
192198
if (!first_index.has_value())
193199
return false;
194200

195201
auto last_index = skeleton.find_last(ch);
196202

197-
builder.append(skeleton.substring_view(*first_index, *last_index - *first_index + 1));
203+
TRY(builder.try_append(skeleton.substring_view(*first_index, *last_index - *first_index + 1)));
198204
return true;
199205
};
200206

201207
for (auto fields : field_order) {
202208
for (auto ch : fields) {
203-
if (append_from_skeleton(first, ch))
209+
if (TRY(append_from_skeleton(first, ch)))
204210
break;
205-
if (append_from_skeleton(second, ch))
211+
if (TRY(append_from_skeleton(second, ch)))
206212
break;
207213
}
208214
}

Userland/Libraries/LibLocale/DateTimeFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ CalendarPatternStyle calendar_pattern_style_from_string(StringView style);
189189
StringView calendar_pattern_style_to_string(CalendarPatternStyle style);
190190

191191
Optional<HourCycleRegion> hour_cycle_region_from_string(StringView hour_cycle_region);
192-
Vector<HourCycle> get_regional_hour_cycles(StringView region);
192+
ErrorOr<Vector<HourCycle>> get_regional_hour_cycles(StringView region);
193193
ErrorOr<Vector<HourCycle>> get_locale_hour_cycles(StringView locale);
194194
ErrorOr<Optional<HourCycle>> get_default_regional_hour_cycle(StringView locale);
195195

0 commit comments

Comments
 (0)