diff --git a/static_core/plugins/ets/runtime/types/ets_string_builder.cpp b/static_core/plugins/ets/runtime/types/ets_string_builder.cpp index 330e9b3b788d65789bc2a916557583915ac42d5d..7346ee5ce4b529eefb111c4d6a35f6b597ffbbc3 100644 --- a/static_core/plugins/ets/runtime/types/ets_string_builder.cpp +++ b/static_core/plugins/ets/runtime/types/ets_string_builder.cpp @@ -53,8 +53,10 @@ static EtsObjectArray *ReallocateBuffer(EtsHandle &bufHandle, ui ASSERT(bufHandle.GetPtr() != nullptr); // Allocate the new buffer - may trigger GC auto *newBuf = EtsObjectArray::Create(bufHandle->GetClass(), bufLen); - /* nothing prevents this assertion from failing! */ - ASSERT(newBuf != nullptr); + /* we need to return and report the OOM exception to ets world */ + if (newBuf == nullptr) { + return nullptr; + } // Copy the old buffer data bufHandle->CopyDataTo(newBuf); EVENT_SB_BUFFER_REALLOC(ManagedThread::GetCurrent()->GetId(), newBuf, newBuf->GetLength(), newBuf->GetElementSize(), @@ -85,6 +87,9 @@ ObjectHeader *AppendCharArrayToBuffer(VMHandle &sbHandle, EtsCharArra EtsHandle bufHandle(coroutine, buf); // May trigger GC buf = ReallocateBuffer(bufHandle, GetNewBufferLength(bufHandle->GetLength(), 1U)); + if (buf == nullptr) { + return nullptr; + } // Update sb and arr as corresponding objects might be moved by GC sb = sbHandle.GetPtr(); arr = arrHandle.GetPtr(); @@ -277,6 +282,9 @@ VMHandle &StringBuilderAppendString(VMHandle &sbHandle, Et EtsHandle bufHandle(coroutine, buf); // May trigger GC buf = ReallocateBuffer(bufHandle, GetNewBufferLength(bufHandle->GetLength(), 1U)); + if (buf == nullptr) { + return sbHandle; + } // Update sb and s as corresponding objects might be moved by GC sb = sbHandle->GetCoreType(); str = strHandle.GetPtr(); @@ -326,6 +334,9 @@ VMHandle &StringBuilderAppendStringsChecked(VMHandle &sbHa EtsHandle bufHandle(coroutine, buf); // May trigger GC buf = ReallocateBuffer(bufHandle, GetNewBufferLength(bufHandle->GetLength(), 2U)); + if (buf == nullptr) { + return sbHandle; + } // Update sb and strings as corresponding objects might be moved by GC sb = sbHandle->GetCoreType(); str0 = str0Handle.GetPtr(); @@ -409,6 +420,9 @@ VMHandle &StringBuilderAppendStringsChecked(VMHandle &sbHa EtsHandle bufHandle(coroutine, buf); // May trigger GC buf = ReallocateBuffer(bufHandle, GetNewBufferLength(bufHandle->GetLength(), 3U)); + if (buf == nullptr) { + return sbHandle; + } // Update sb and strings as corresponding objects might be moved by GC sb = sbHandle->GetCoreType(); str0 = str0Handle.GetPtr(); @@ -507,6 +521,9 @@ VMHandle &StringBuilderAppendStringsChecked(VMHandle &sbHa EtsHandle bufHandle(coroutine, buf); // May trigger GC buf = ReallocateBuffer(bufHandle, GetNewBufferLength(bufHandle->GetLength(), 4U)); + if (buf == nullptr) { + return sbHandle; + } // Update sb and strings as corresponding objects might be moved by GC sb = sbHandle->GetCoreType(); str0 = str0Handle.GetPtr();