From 46521516300f7e664598145cffc421ac6d307be8 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Tue, 20 Aug 2024 19:49:13 +0200 Subject: [PATCH 1/2] 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) { From aa808d63bc68e0e52970c524c7aad758a5cf15a7 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Wed, 21 Aug 2024 16:40:39 +0200 Subject: [PATCH 2/2] Return nullptr straight away --- components/lua/luastate.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/lua/luastate.cpp b/components/lua/luastate.cpp index 4705be5831..889834edc8 100644 --- a/components/lua/luastate.cpp +++ b/components/lua/luastate.cpp @@ -111,8 +111,7 @@ namespace LuaUtil if (!newPtr) { Log(Debug::Error) << "Lua realloc " << osize << "->" << nsize << " failed"; - smallAllocDelta = 0; - bigAllocDelta = 0; + return nullptr; } } self->mTotalMemoryUsage += smallAllocDelta + bigAllocDelta;