From 6f6b90166d31cab04b12391aed45855c98715e44 Mon Sep 17 00:00:00 2001 From: lijincheng Date: Thu, 10 Jul 2025 09:55:21 +0800 Subject: [PATCH] Bugfix for stringbuilder 1.return when oom happens Issue:https://gitee.com/openharmony/arkcompiler_runtime_core/issues/ICLAK2 Signed-off-by: lijincheng --- .../ets/runtime/types/ets_string_builder.cpp | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) 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 330e9b3b78..7346ee5ce4 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(); -- Gitee