From 46521516300f7e664598145cffc421ac6d307be8 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Tue, 20 Aug 2024 19:49:13 +0200 Subject: [PATCH] Don't assume realloc always succeeds --- components/lua/luastate.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/components/lua/luastate.cpp b/components/lua/luastate.cpp index 26b832bdf9..4705be5831 100644 --- a/components/lua/luastate.cpp +++ b/components/lua/luastate.cpp @@ -101,14 +101,22 @@ namespace LuaUtil << " is blocked because Lua memory limit (configurable in settings.cfg) is exceeded"; return nullptr; } - self->mTotalMemoryUsage += smallAllocDelta + bigAllocDelta; - self->mSmallAllocMemoryUsage += smallAllocDelta; void* newPtr = nullptr; if (nsize == 0) free(ptr); else + { newPtr = realloc(ptr, nsize); + if (!newPtr) + { + Log(Debug::Error) << "Lua realloc " << osize << "->" << nsize << " failed"; + smallAllocDelta = 0; + bigAllocDelta = 0; + } + } + self->mTotalMemoryUsage += smallAllocDelta + bigAllocDelta; + self->mSmallAllocMemoryUsage += smallAllocDelta; if (bigAllocDelta != 0) {