From ee6991e6d899f0023ee3437c8922d03854ccc523 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 25 Oct 2024 07:54:49 +0200 Subject: [PATCH] Do a check if a local variable exceeds the available stack space. Windows stack is 1 MB so play it safe and allow 512 kb as max. stack space for a single function. --- src/common/scripting/backend/codegen.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/common/scripting/backend/codegen.cpp b/src/common/scripting/backend/codegen.cpp index b14fb267cd..df1c48c22e 100644 --- a/src/common/scripting/backend/codegen.cpp +++ b/src/common/scripting/backend/codegen.cpp @@ -12640,6 +12640,15 @@ FxExpression *FxLocalVariableDeclaration::Resolve(FCompileContext &ctx) if (ValueType->RegType == REGT_NIL && ValueType != TypeAuto) { auto sfunc = static_cast(ctx.Function->Variants[0].Implementation); + + const unsigned MAX_STACK_ALLOC = 512 * 1024; // Windows stack is 1 MB, but we cannot go up there without problems + if (uint64_t(ValueType->Size) + uint64_t(sfunc->ExtraSpace) > MAX_STACK_ALLOC) + { + ScriptPosition.Message(MSG_ERROR, "%s exceeds max. allowed size of 512kb for local variables at variable %s", sfunc->Name.GetChars(), Name.GetChars()); + delete this; + return nullptr; + } + StackOffset = sfunc->AllocExtraStack(ValueType); if (Init != nullptr)