mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-05-31 23:42:52 +00:00
LibJS: Throw RangeError in StringPrototype::repeat
if OOM
currently crashes with an assertion failure in `String::repeated` if malloc can't serve a `count * input_size` sized request, so add `String::repeated_with_error` to propagate the error.
This commit is contained in:
parent
f4c8f1a346
commit
ecb7d4b40f
Notes:
sideshowbarker
2024-07-18 00:34:07 +09:00
Author: https://github.com/ttrssreal
Commit: ecb7d4b40f
Pull-request: https://github.com/SerenityOS/serenity/pull/24010
Reviewed-by: https://github.com/Hendiadyoin1
Reviewed-by: https://github.com/trflynn89 ✅
5 changed files with 16 additions and 5 deletions
|
@ -308,13 +308,14 @@ bool String::equals_ignoring_ascii_case(StringView other) const
|
|||
return StringUtils::equals_ignoring_ascii_case(bytes_as_string_view(), other);
|
||||
}
|
||||
|
||||
String String::repeated(String const& input, size_t count)
|
||||
ErrorOr<String> String::repeated(String const& input, size_t count)
|
||||
{
|
||||
VERIFY(!Checked<size_t>::multiplication_would_overflow(count, input.bytes().size()));
|
||||
if (Checked<size_t>::multiplication_would_overflow(count, input.bytes().size()))
|
||||
return Error::from_errno(EOVERFLOW);
|
||||
|
||||
String result;
|
||||
size_t input_size = input.bytes().size();
|
||||
MUST(result.replace_with_new_string(count * input_size, [&](Bytes buffer) {
|
||||
TRY(result.replace_with_new_string(count * input_size, [&](Bytes buffer) {
|
||||
if (input_size == 1) {
|
||||
buffer.fill(input.bytes().first());
|
||||
} else {
|
||||
|
@ -323,6 +324,7 @@ String String::repeated(String const& input, size_t count)
|
|||
}
|
||||
return ErrorOr<void> {};
|
||||
}));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue