tighter tests for stack overflow

This commit is contained in:
Roberto Ierusalimschy
2004-09-08 11:23:09 -03:00
parent ee165043ef
commit 0de2065f4e
4 changed files with 37 additions and 24 deletions

17
lgc.c
View File

@@ -1,5 +1,5 @@
/*
** $Id: lgc.c,v 2.9 2004/08/24 20:12:06 roberto Exp roberto $
** $Id: lgc.c,v 2.10 2004/08/30 13:44:44 roberto Exp roberto $
** Garbage Collector
** See Copyright Notice in lua.h
*/
@@ -239,14 +239,17 @@ static void traverseclosure (global_State *g, Closure *cl) {
static void checkstacksizes (lua_State *L, StkId max) {
int used = L->ci - L->base_ci; /* number of `ci' in use */
if (4*used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)
int ci_used = L->ci - L->base_ci; /* number of `ci' in use */
int s_used = max - L->stack; /* part of stack in use */
if (L->size_ci > LUA_MAXCALLS) /* handling overflow? */
return; /* do not touch the stacks */
if (4*ci_used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci)
luaD_reallocCI(L, L->size_ci/2); /* still big enough... */
else condhardstacktests(luaD_reallocCI(L, L->size_ci));
used = max - L->stack; /* part of stack in use */
if (4*used < L->stacksize && 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize)
condhardstacktests(luaD_reallocCI(L, ci_used + 1));
if (4*s_used < L->stacksize &&
2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize)
luaD_reallocstack(L, L->stacksize/2); /* still big enough... */
else condhardstacktests(luaD_reallocstack(L, L->stacksize));
condhardstacktests(luaD_reallocstack(L, s_used));
}