From 4a00f61276a9a38b0427fbae3dbbd86dfb5a0749 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 23 May 2022 10:38:03 -0300 Subject: 'lua_checkstack' doesn't need to check stack overflow 'luaD_growstack' already checks that. This commit also fixes an internal bug in 'luaD_growstack': a large 'n' could cause an arithmetic overflow when computing 'needed'. --- ldo.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'ldo.c') diff --git a/ldo.c b/ldo.c index 8e4faf02..5aa6d59d 100644 --- a/ldo.c +++ b/ldo.c @@ -227,7 +227,7 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { luaD_throw(L, LUA_ERRERR); /* error inside message handler */ return 0; /* if not 'raiseerror', just signal it */ } - else { + else if (n < LUAI_MAXSTACK) { /* avoids arithmetic overflows */ int newsize = 2 * size; /* tentative new size */ int needed = cast_int(L->top - L->stack) + n; if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ @@ -236,14 +236,13 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) { newsize = needed; if (l_likely(newsize <= LUAI_MAXSTACK)) return luaD_reallocstack(L, newsize, raiseerror); - else { /* stack overflow */ - /* add extra size to be able to handle the error message */ - luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); - if (raiseerror) - luaG_runerror(L, "stack overflow"); - return 0; - } } + /* else stack overflow */ + /* add extra size to be able to handle the error message */ + luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror); + if (raiseerror) + luaG_runerror(L, "stack overflow"); + return 0; } -- cgit v1.2.3-55-g6feb