From 59a1adf194efe43741c2bb2005d93d8320a19d14 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 1 Jul 2025 10:57:02 -0300 Subject: LUAI_MAXSTACK defined privately LUAI_MAXSTACK is limited to INT_MAX/2, so can use INT_MAX/2 to define pseudo-indices (LUA_REGISTRYINDEX) in 'lua.h'. A change in the maximum stack size does not need to change the Lua-C ABI. --- ldo.c | 14 ++++++++++++++ ltests.h | 1 - lua.h | 6 +++--- luaconf.h | 14 -------------- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/ldo.c b/ldo.c index 776519dc..f232b588 100644 --- a/ldo.c +++ b/ldo.c @@ -174,6 +174,20 @@ TStatus luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) { #define STACKERRSPACE 200 +/* +** LUAI_MAXSTACK limits the size of the Lua stack. +** It must fit into INT_MAX/2. +*/ + +#if !defined(LUAI_MAXSTACK) +#if 1000000 < (INT_MAX / 2) +#define LUAI_MAXSTACK 1000000 +#else +#define LUAI_MAXSTACK (INT_MAX / 2u) +#endif +#endif + + /* maximum stack size that respects size_t */ #define MAXSTACK_BYSIZET ((MAX_SIZET / sizeof(StackValue)) - STACKERRSPACE) diff --git a/ltests.h b/ltests.h index 43f08162..d34e9d42 100644 --- a/ltests.h +++ b/ltests.h @@ -155,7 +155,6 @@ LUA_API void *debug_realloc (void *ud, void *block, ** Reduce maximum stack size to make stack-overflow tests run faster. ** (But value is still large enough to overflow smaller integers.) */ -#undef LUAI_MAXSTACK #define LUAI_MAXSTACK 68000 diff --git a/lua.h b/lua.h index 95e0db32..131a8fcb 100644 --- a/lua.h +++ b/lua.h @@ -37,10 +37,10 @@ /* ** Pseudo-indices -** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty -** space after that to help overflow detection) +** (The stack size is limited to INT_MAX/2; we keep some free empty +** space after that to help overflow detection.) */ -#define LUA_REGISTRYINDEX (-LUAI_MAXSTACK - 1000) +#define LUA_REGISTRYINDEX (-(INT_MAX/2 + 1000)) #define lua_upvalueindex(i) (LUA_REGISTRYINDEX - (i)) diff --git a/luaconf.h b/luaconf.h index 51e77547..bc5fbe9f 100644 --- a/luaconf.h +++ b/luaconf.h @@ -763,20 +763,6 @@ ** ===================================================================== */ -/* -@@ LUAI_MAXSTACK limits the size of the Lua stack. -** CHANGE it if you need a different limit. This limit is arbitrary; -** its only purpose is to stop Lua from consuming unlimited stack -** space and to reserve some numbers for pseudo-indices. -** (It must fit into max(int)/2.) -*/ -#if 1000000 < (INT_MAX / 2) -#define LUAI_MAXSTACK 1000000 -#else -#define LUAI_MAXSTACK (INT_MAX / 2u) -#endif - - /* @@ LUA_EXTRASPACE defines the size of a raw memory area associated with ** a Lua state with very fast access. -- cgit v1.2.3-55-g6feb