[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Dangerous to call luaL_checkstack when the stack is saturated?
- From: Roberto Ierusalimschy <roberto@...>
- Date: Fri, 4 Dec 2015 09:54:17 -0200
> 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
pushes.
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