aboutsummaryrefslogtreecommitdiff
path: root/ldo.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-12-11 10:43:40 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2017-12-11 10:43:40 -0200
commitbfb88e99e9cbc492b35e5dc53594b1d1216171cd (patch)
tree4e63c15daa2f27cbba2c7cd4d301eea52e712c4f /ldo.c
parentc5ebed73997c118306e548ac5ccb98302dbf90e4 (diff)
downloadlua-bfb88e99e9cbc492b35e5dc53594b1d1216171cd.tar.gz
lua-bfb88e99e9cbc492b35e5dc53594b1d1216171cd.tar.bz2
lua-bfb88e99e9cbc492b35e5dc53594b1d1216171cd.zip
'luaD_growstack' cannot raise any errors when 'raiseerror' is
false (+ some comments)
Diffstat (limited to 'ldo.c')
-rw-r--r--ldo.c41
1 files changed, 27 insertions, 14 deletions
diff --git a/ldo.c b/ldo.c
index 136aef55..a4a2ecbf 100644
--- a/ldo.c
+++ b/ldo.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldo.c,v 2.177 2017/12/01 15:44:51 roberto Exp roberto $ 2** $Id: ldo.c,v 2.178 2017/12/08 17:28:25 roberto Exp roberto $
3** Stack and Call structure of Lua 3** Stack and Call structure of Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -177,14 +177,15 @@ static void correctstack (lua_State *L, StkId oldstack, StkId newstack) {
177#define ERRORSTACKSIZE (LUAI_MAXSTACK + 200) 177#define ERRORSTACKSIZE (LUAI_MAXSTACK + 200)
178 178
179 179
180int luaD_reallocstack (lua_State *L, int newsize, int safe) { 180int luaD_reallocstack (lua_State *L, int newsize, int raiseerror) {
181 int lim = L->stacksize; 181 int lim = L->stacksize;
182 StkId newstack = luaM_reallocvector(L, L->stack, lim, newsize, StackValue); 182 StkId newstack = luaM_reallocvector(L, L->stack, lim, newsize, StackValue);
183 lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE); 183 lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
184 lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK); 184 lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK);
185 if (newstack == NULL) { /* reallocation failed? */ 185 if (newstack == NULL) { /* reallocation failed? */
186 if (safe) luaM_error(L); 186 if (raiseerror)
187 else return 0; /* no-safe mode: signal the error */ 187 luaM_error(L);
188 else return 0; /* do not raise an error */
188 } 189 }
189 for (; lim < newsize; lim++) 190 for (; lim < newsize; lim++)
190 setnilvalue(s2v(newstack + lim)); /* erase new segment */ 191 setnilvalue(s2v(newstack + lim)); /* erase new segment */
@@ -196,21 +197,33 @@ int luaD_reallocstack (lua_State *L, int newsize, int safe) {
196} 197}
197 198
198 199
199int luaD_growstack (lua_State *L, int n, int safe) { 200/*
201** Try to grow the stack by at least 'n' elements. when 'raiseerror'
202** is true, raises any error; otherwise, return 0 in case of errors.
203*/
204int luaD_growstack (lua_State *L, int n, int raiseerror) {
200 int size = L->stacksize; 205 int size = L->stacksize;
201 int newsize = 2 * size; 206 int newsize = 2 * size; /* tentative new size */
202 if (size > LUAI_MAXSTACK) /* error after extra size? */ 207 if (size > LUAI_MAXSTACK) { /* need more space after extra size? */
203 luaD_throw(L, LUA_ERRERR); 208 if (raiseerror)
209 luaD_throw(L, LUA_ERRERR); /* error inside message handler */
210 else return 0;
211 }
204 else { 212 else {
205 int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK; 213 int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK;
206 if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK; 214 if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */
207 if (newsize < needed) newsize = needed; 215 newsize = LUAI_MAXSTACK;
216 if (newsize < needed) /* but must respect what was asked for */
217 newsize = needed;
208 if (newsize > LUAI_MAXSTACK) { /* stack overflow? */ 218 if (newsize > LUAI_MAXSTACK) { /* stack overflow? */
209 luaD_reallocstack(L, ERRORSTACKSIZE, 1); 219 /* add extra size to be able to handle the error message */
210 luaG_runerror(L, "stack overflow"); 220 luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror);
221 if (raiseerror)
222 luaG_runerror(L, "stack overflow");
223 else return 0;
211 } 224 }
212 } /* else */ 225 } /* else no errors */
213 return luaD_reallocstack(L, newsize, safe); 226 return luaD_reallocstack(L, newsize, raiseerror);
214} 227}
215 228
216 229