Skip to content

Commit 3e928d9

Browse files
authored
Save stack space while handling errors (#757)
1 parent 66b8a97 commit 3e928d9

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/lua/src/ldebug.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,11 @@ l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
656656
va_start(argp, fmt);
657657
msg = luaO_pushvfstring(L, fmt, argp); /* format message */
658658
va_end(argp);
659-
if (isLua(ci)) /* if Lua function, add source:line information */
659+
if (isLua(ci)) { /* if Lua function, add source:line information */
660660
luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci));
661+
setobjs2s(L, L->top - 2, L->top - 1); /* remove 'msg' from the stack */
662+
L->top--;
663+
}
661664
luaG_errormsg(L);
662665
}
663666

src/lua/src/lvm.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,10 @@ void luaV_concat (lua_State *L, int total) {
490490
/* collect total length and number of strings */
491491
for (n = 1; n < total && tostring(L, top - n - 1); n++) {
492492
size_t l = vslen(top - n - 1);
493-
if (l >= (MAX_SIZE/sizeof(char)) - tl)
493+
if (l >= (MAX_SIZE/sizeof(char)) - tl) {
494+
L->top = top - total; /* pop strings to avoid wasting stack */
494495
luaG_runerror(L, "string length overflow");
496+
}
495497
tl += l;
496498
}
497499
if (tl <= LUAI_MAXSHORTLEN) { /* is result a short string? */
@@ -506,7 +508,7 @@ void luaV_concat (lua_State *L, int total) {
506508
setsvalue2s(L, top - n, ts); /* create result */
507509
}
508510
total -= n-1; /* got 'n' strings to create 1 new */
509-
L->top -= n-1; /* popped 'n' strings and pushed one */
511+
L->top = top - (n - 1); /* popped 'n' strings and pushed one */
510512
} while (total > 1); /* repeat until only 1 result left */
511513
}
512514

0 commit comments

Comments
 (0)