aboutsummaryrefslogtreecommitdiff
path: root/ldo.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-05-23 10:38:03 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2022-05-23 10:38:03 -0300
commit4a00f61276a9a38b0427fbae3dbbd86dfb5a0749 (patch)
tree16ed717a2f4b79bad0743c2a8888ba55e013a309 /ldo.c
parent42d40581dd919fb134c07027ca1ce0844c670daf (diff)
downloadlua-4a00f61276a9a38b0427fbae3dbbd86dfb5a0749.tar.gz
lua-4a00f61276a9a38b0427fbae3dbbd86dfb5a0749.tar.bz2
lua-4a00f61276a9a38b0427fbae3dbbd86dfb5a0749.zip
'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'.
Diffstat (limited to 'ldo.c')
-rw-r--r--ldo.c15
1 files changed, 7 insertions, 8 deletions
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) {
227 luaD_throw(L, LUA_ERRERR); /* error inside message handler */ 227 luaD_throw(L, LUA_ERRERR); /* error inside message handler */
228 return 0; /* if not 'raiseerror', just signal it */ 228 return 0; /* if not 'raiseerror', just signal it */
229 } 229 }
230 else { 230 else if (n < LUAI_MAXSTACK) { /* avoids arithmetic overflows */
231 int newsize = 2 * size; /* tentative new size */ 231 int newsize = 2 * size; /* tentative new size */
232 int needed = cast_int(L->top - L->stack) + n; 232 int needed = cast_int(L->top - L->stack) + n;
233 if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */ 233 if (newsize > LUAI_MAXSTACK) /* cannot cross the limit */
@@ -236,14 +236,13 @@ int luaD_growstack (lua_State *L, int n, int raiseerror) {
236 newsize = needed; 236 newsize = needed;
237 if (l_likely(newsize <= LUAI_MAXSTACK)) 237 if (l_likely(newsize <= LUAI_MAXSTACK))
238 return luaD_reallocstack(L, newsize, raiseerror); 238 return luaD_reallocstack(L, newsize, raiseerror);
239 else { /* stack overflow */
240 /* add extra size to be able to handle the error message */
241 luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror);
242 if (raiseerror)
243 luaG_runerror(L, "stack overflow");
244 return 0;
245 }
246 } 239 }
240 /* else stack overflow */
241 /* add extra size to be able to handle the error message */
242 luaD_reallocstack(L, ERRORSTACKSIZE, raiseerror);
243 if (raiseerror)
244 luaG_runerror(L, "stack overflow");
245 return 0;
247} 246}
248 247
249 248