1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:07:35 +00:00

LibJS: Pre-allocate the out-of-memory error string on the VM

If we are out of memory, we can't try to allocate a string that could
fail as well. When Error is converted to String, this would result in an
endless OOM-throwing loop. Instead, pre-allocate the string on the VM,
and use it to construct the Error.

Note that as of this commit, the OOM string is still a DeprecatedString.
This is just preporatory for Error's conversion to String.
This commit is contained in:
Timothy Flynn 2023-02-16 12:55:22 -05:00 committed by Tim Flynn
parent 93ad25fbe5
commit 4d10911f96
5 changed files with 42 additions and 31 deletions

View file

@ -30,12 +30,7 @@ public:
ThrowCompletionOr<void> appendff(CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters)
{
AK::VariadicFormatParams<AK::AllowDebugOnlyFormatters::No, Parameters...> variadic_format_params { parameters... };
if (vformat(*this, fmtstr.view(), variadic_format_params).is_error()) {
// The size returned here is a bit of an estimate, as we don't know what the final formatted string length would be.
return m_vm.throw_completion<InternalError>(ErrorType::NotEnoughMemoryToAllocate, length() + fmtstr.view().length());
}
TRY_OR_THROW_OOM(m_vm, vformat(*this, fmtstr.view(), variadic_format_params));
return {};
}