'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'.
This commit is contained in:
Roberto Ierusalimschy
2022-05-23 10:38:03 -03:00
parent 42d40581dd
commit 4a00f61276
5 changed files with 17 additions and 26 deletions

9
lapi.c
View File

@@ -114,13 +114,8 @@ LUA_API int lua_checkstack (lua_State *L, int n) {
api_check(L, n >= 0, "negative 'n'");
if (L->stack_last - L->top > n) /* stack large enough? */
res = 1; /* yes; check is OK */
else { /* no; need to grow stack */
int inuse = cast_int(L->top - L->stack) + EXTRA_STACK;
if (inuse > LUAI_MAXSTACK - n) /* can grow without overflow? */
res = 0; /* no */
else /* try to grow stack */
res = luaD_growstack(L, n, 0);
}
else /* need to grow stack */
res = luaD_growstack(L, n, 0);
if (res && ci->top < L->top + n)
ci->top = L->top + n; /* adjust frame top */
lua_unlock(L);