lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

I've heard of EXTRA_STACK before but I'm nervous about relying on it. The (5.1) implementation of lua_checkstack doesn't seem to factor EXTRA_STACK into its decision to return false.

On Fri, Dec 4, 2015 at 5:54 AM, Roberto Ierusalimschy <> wrote:
> Am 04.12.2015 um 08:58 schröbte DeathByNukes:
> >Looking at the implementation of luaL_checkstack, it merely calls
> >lua_checkstack and immediately calls luaL_error if that returns
> >false. luaL_error internally pushes 2 values on the stack without checking.
> >
> >Shouldn't there be a call to lua_checkstack(L,2) somewhere in there so it
> >can conditionally pop 2 values from the stack before proceeding?
> >
> Lua allocates 5 additional stack slots for internal use in Lua API
> functions. See `EXTRA_STACK` in `lstate.h`.

This ensures that there is no real danger of a runtime bug, but the OP
is right in the sense that, conceptually, this extra space should not
be available to the API users. (It is for internal use. So, lua_error
can use these slots, but luaL_error should not.)  In particular, if
LUA_USE_APICHECK is on, it might accuse an API violation in those

Nevertheless, that is quite rare in practice. luaL_checkstack always
ensures more space than you asked, so that sitation could only occur in
the first call to luaL_checkstack, with at least (MINSTACK-2) elements
already pushed before the call. Probably the best fix is to document
that (that is, that if you are filling the stack, you should start
calling luaL_checkstack before it is too late :-).

-- Roberto