From caf00e61d8c53d759b6b7c5d6c3a3ce38d0ea5a5 Mon Sep 17 00:00:00 2001 From: npt-1707 Date: Sun, 18 May 2025 20:09:47 +0800 Subject: [PATCH] depends/lua/src/ldebug.c: Save stack space while handling errors --- depends/lua/src/ldebug.c | 5 ++++- depends/lua/src/lvm.c | 6 ++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/depends/lua/src/ldebug.c b/depends/lua/src/ldebug.c index bb0e1d4ace..a53578480c 100644 --- a/depends/lua/src/ldebug.c +++ b/depends/lua/src/ldebug.c @@ -658,8 +658,11 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { va_start(argp, fmt); msg = luaO_pushvfstring(L, fmt, argp); /* format message */ va_end(argp); - if (isLua(ci)) /* if Lua function, add source:line information */ + if (isLua(ci)) { /* if Lua function, add source:line information */ luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci)); + setobjs2s(L, L->top - 2, L->top - 1); /* remove 'msg' from the stack */ + L->top--; + } luaG_errormsg(L); } diff --git a/depends/lua/src/lvm.c b/depends/lua/src/lvm.c index cc43d8714d..66fe8baca2 100644 --- a/depends/lua/src/lvm.c +++ b/depends/lua/src/lvm.c @@ -490,8 +490,10 @@ void luaV_concat (lua_State *L, int total) { /* collect total length and number of strings */ for (n = 1; n < total && tostring(L, top - n - 1); n++) { size_t l = vslen(top - n - 1); - if (l >= (MAX_SIZE/sizeof(char)) - tl) + if (l_unlikely(l >= (MAX_SIZE/sizeof(char)) - tl)) { + L->top = top - total; /* pop strings to avoid wasting stack */ luaG_runerror(L, "string length overflow"); + } tl += l; } if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */ @@ -506,7 +508,7 @@ void luaV_concat (lua_State *L, int total) { setsvalue2s(L, top - n, ts); /* create result */ } total -= n-1; /* got 'n' strings to create 1 new */ - L->top -= n-1; /* popped 'n' strings and pushed one */ + L->top = top - (n - 1); /* popped 'n' strings and pushed one */ } while (total > 1); /* repeat until only 1 result left */ }