'luaD_growstack' cannot raise any errors when 'raiseerror' is

false (+ some comments)
This commit is contained in:
Roberto Ierusalimschy 2017-12-11 10:43:40 -02:00
parent c5ebed7399
commit bfb88e99e9
2 changed files with 30 additions and 17 deletions

41
ldo.c
View File

@ -1,5 +1,5 @@
/*
** $Id: ldo.c,v 2.177 2017/12/01 15:44:51 roberto Exp roberto $
** $Id: ldo.c,v 2.178 2017/12/08 17:28:25 roberto Exp roberto $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@ -177,14 +177,15 @@ static void correctstack (lua_State *L, StkId oldstack, StkId newstack) {
#define ERRORSTACKSIZE (LUAI_MAXSTACK + 200)
int luaD_reallocstack (lua_State *L, int newsize, int safe) {
int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) {
int lim = L->stacksize;
StkId newstack = luaM_reallocvector(L, L->stack, lim, newsize, StackValue);
lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK);
if (newstack == NULL) { /* reallocation failed? */
if (safe) luaM_error(L);
else return 0; /* no-safe mode: signal the error */
if (raiseerror)
luaM_error(L);
else return 0; /* do not raise an error */
}
for (; lim < newsize; lim++)
setnilvalue(s2v(newstack + lim)); /* erase new segment */
@ -196,21 +197,33 @@ int luaD_reallocstack (lua_State *L, int newsize, int safe) {
}
int luaD_growstack (lua_State *L, int n, int safe) {
/*
** Try to grow the stack by at least 'n' elements. when 'raiseerror'
** is true, raises any error; otherwise, return 0 in case of errors.
*/
int luaD_growstack (lua_State *L, int n, int raiseerror) {
int size = L->stacksize;
int newsize = 2 * size;
if (size > LUAI_MAXSTACK) /* error after extra size? */
luaD_throw(L, LUA_ERRERR);
int newsize = 2 * size; /* tentative new size */
if (size > LUAI_MAXSTACK) { /* need more space after extra size? */
if (raiseerror)
luaD_throw(L, LUA_ERRERR); /* error inside message handler */
else return 0;
}
else {
int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK;
if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK;
if (newsize < needed) newsize = needed;
if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */
newsize = LUAI_MAXSTACK;
if (newsize < needed) /* but must respect what was asked for */
newsize = needed;
if (newsize > LUAI_MAXSTACK) { /* stack overflow? */
luaD_reallocstack(L, ERRORSTACKSIZE, 1);
luaG_runerror(L, "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");
else return 0;
}
} /* else */
return luaD_reallocstack(L, newsize, safe);
} /* else no errors */
return luaD_reallocstack(L, newsize, raiseerror);
}

6
ldo.h
View File

@ -1,5 +1,5 @@
/*
** $Id: ldo.h,v 2.36 2017/11/23 18:29:41 roberto Exp roberto $
** $Id: ldo.h,v 2.37 2017/12/08 17:28:25 roberto Exp roberto $
** Stack and Call structure of Lua
** See Copyright Notice in lua.h
*/
@ -56,8 +56,8 @@ LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u,
ptrdiff_t oldtop, ptrdiff_t ef);
LUAI_FUNC void luaD_poscall (lua_State *L, CallInfo *ci, StkId firstResult,
int nres);
LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int safe);
LUAI_FUNC int luaD_growstack (lua_State *L, int n, int safe);
LUAI_FUNC int luaD_reallocstack (lua_State *L, int newsize, int raiseerror);
LUAI_FUNC int luaD_growstack (lua_State *L, int n, int raiseerror);
LUAI_FUNC void luaD_shrinkstack (lua_State *L);
LUAI_FUNC void luaD_inctop (lua_State *L);