Browse Source

LUAI_MAXCCALLS renamed LUAI_MAXCSTACK

The limit LUAI_MAXCCALLS was renamed LUAI_MAXCSTACK, which better
represents its meaning. Moreover, its definition was moved to
'luaconf.h', given its importance now that Lua does not use
a "stackless" implementation.
pull/22/head
Roberto Ierusalimschy 6 years ago
parent
commit
0443ad9e28
  1. 4
      ldo.c
  2. 9
      llimits.h
  3. 16
      lstate.c
  4. 4
      ltests.h
  5. 15
      luaconf.h

4
ldo.c

@ -521,7 +521,7 @@ void luaD_call (lua_State *L, StkId func, int nresults) {
*/
void luaD_callnoyield (lua_State *L, StkId func, int nResults) {
incXCcalls(L);
if (getCcalls(L) >= LUAI_MAXCCALLS) /* possible stack overflow? */
if (getCcalls(L) >= LUAI_MAXCSTACK) /* possible stack overflow? */
luaE_freeCI(L);
luaD_call(L, func, nResults);
decXCcalls(L);
@ -673,7 +673,7 @@ LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs,
L->nCcalls = 1;
else /* correct 'nCcalls' for this thread */
L->nCcalls = getCcalls(from) - from->nci + L->nci + CSTACKCF;
if (L->nCcalls >= LUAI_MAXCCALLS)
if (L->nCcalls >= LUAI_MAXCSTACK)
return resume_error(L, "C stack overflow", nargs);
luai_userstateresume(L, nargs);
api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);

9
llimits.h

@ -168,15 +168,6 @@ typedef LUAI_UACINT l_uacInt;
#endif
/*
** maximum depth for nested C calls and syntactical nested non-terminals
** in a program. (Value must fit in an unsigned short int. It must also
** be compatible with the size of the C stack.)
*/
#if !defined(LUAI_MAXCCALLS)
#define LUAI_MAXCCALLS 2200
#endif
/*

16
lstate.c

@ -100,13 +100,13 @@ void luaE_setdebt (global_State *g, l_mem debt) {
** Increment count of "C calls" and check for overflows. In case of
** a stack overflow, check appropriate error ("regular" overflow or
** overflow while handling stack overflow).
** If 'nCcalls' is larger than LUAI_MAXCCALLS but smaller than
** LUAI_MAXCCALLS + CSTACKCF (plus 2 to avoid by-one errors), it means
** If 'nCcalls' is larger than LUAI_MAXCSTACK but smaller than
** LUAI_MAXCSTACK + CSTACKCF (plus 2 to avoid by-one errors), it means
** it has just entered the "overflow zone", so the function raises an
** overflow error.
** If 'nCcalls' is larger than LUAI_MAXCCALLS + CSTACKCF + 2
** If 'nCcalls' is larger than LUAI_MAXCSTACK + CSTACKCF + 2
** (which means it is already handling an overflow) but smaller than
** 9/8 of LUAI_MAXCCALLS, does not report an error (to allow message
** 9/8 of LUAI_MAXCSTACK, does not report an error (to allow message
** handling to work).
** Otherwise, report a stack overflow while handling a stack overflow
** (probably caused by a repeating error in the message handling
@ -115,16 +115,16 @@ void luaE_setdebt (global_State *g, l_mem debt) {
void luaE_enterCcall (lua_State *L) {
int ncalls = getCcalls(L);
L->nCcalls++;
if (ncalls >= LUAI_MAXCCALLS) { /* possible overflow? */
if (ncalls >= LUAI_MAXCSTACK) { /* possible overflow? */
luaE_freeCI(L); /* release unused CIs */
ncalls = getCcalls(L); /* update call count */
if (ncalls >= LUAI_MAXCCALLS) { /* still overflow? */
if (ncalls <= LUAI_MAXCCALLS + CSTACKCF + 2) {
if (ncalls >= LUAI_MAXCSTACK) { /* still overflow? */
if (ncalls <= LUAI_MAXCSTACK + CSTACKCF + 2) {
/* no error before increments; raise the error now */
L->nCcalls += (CSTACKCF + 4); /* avoid raising it again */
luaG_runerror(L, "C stack overflow");
}
else if (ncalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS >> 3)))
else if (ncalls >= (LUAI_MAXCSTACK + (LUAI_MAXCSTACK >> 3)))
luaD_throw(L, LUA_ERRERR); /* error while handling stack error */
}
}

4
ltests.h

@ -30,8 +30,8 @@
/* compiled with -O0, Lua uses a lot of C stack space... */
#undef LUAI_MAXCCALLS
#define LUAI_MAXCCALLS 400
#undef LUAI_MAXCSTACK
#define LUAI_MAXCSTACK 400
/* to avoid warnings, and to make sure value is really unused */
#define UNUSED(x) (x=0, (void)(x))

15
luaconf.h

@ -27,6 +27,21 @@
** =====================================================================
*/
/*
@@ LUAI_MAXCSTACK defines the maximum depth for nested calls and
** also limits the maximum depth of other recursive algorithms in
** the implementation, such as syntactic analysis. A value too
** large may allow the interpreter to crash (C-stack overflow).
** The default value seems ok for regular machines, but may be
** too high for restricted hardware.
** The test file 'cstack.lua' may help finding a good limit.
** (It will crash with a limit too high.)
*/
#if !defined(LUAI_MAXCSTACK)
#define LUAI_MAXCSTACK 2200
#endif
/*
@@ LUA_32BITS enables Lua with 32-bit integers and 32-bit floats. You
** can also define LUA_32BITS in the make file, but changing here you

Loading…
Cancel
Save