[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: LuaJIT asserts on negative non-existent indexes
- From: Mike Pall <mikelu-1004@...>
- Date: Sun, 25 Apr 2010 21:16:28 +0200
Arseny Vakhrushev wrote:
> I'm still playing with LuaJIT and have encountered a peculiar
> LuaJIT behavior. If I call lua_is*() or lua_to*() function
> (lua_toboolean() or lua_touserdata() in my case) with a negative
> non-existent index, assertion fails:
>
> test2: lj_api.c:39: index2adr: Assertion `idx != 0 && -idx <= L->top - L->base' failed.
If you compile plain Lua with API checks (i.e. -DLUA_USE_APICHECK),
you'll get a similar assertion (I just tried).
> However, if I do the same with a positive but still non-existent
> index, assertion succeeds. Like:
>
> lua_State* L = luaL_newstate();
> lua_toboolean(L, 10); // alright
> lua_toboolean(L, -10); // <<-- assertion fails here
The positive index is still within the maximum stack assigned to
the C function (by default 20 slots beyond the last argument).
Try again with 50 and you'll get an assertion, too.
> For lua_toboolean(), the manual states that
>
> "It also returns 0 when called with a non-valid index."
You may want to re-read sections 3.1 and 3.2 of the Lua manual.
These define what an "acceptable" and what a "valid" index is.
Crude ASCII diagram follows:
stack base stack top stack max
V V V
... -7 -6|-5 -4 -3 -2 -1| |
|## ## ## ## ##| |
|+1 +2 +3 +4 +5|+6 +7 +8 +9 ... +20|+21 +22 ...
\------------/
valid indexes
\--------------------------------/
acceptable indexes
So you are allowed to access an "acceptable, non-valid index" but
not an "unacceptable, non-valid index". Yay! I guess the Lua
manual could need some terminology cleanup. ;-)
Just to make it clear: the current implementations of both Lua 5.1
and LuaJIT agree on that point. I.e. it's not a bug.
> So, I suppose I'm doing a legal thing trying to fetch a value at
> the end (-1) without checking a stack size. Probably, it's not a
> big deal, and LuaJIT will still work fine with suppressed
> assertions, but I really like this mode to make sure I do
> everything right.
No, it's not safe to do this. Suppressing the assertion just hides
that it's accessing potentially random memory locations.
--Mike