lua-users home
lua-l archive

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


Hello!

I'm fairly new to lua and when doing tests I hit one thing that took me
a while to figure out.

According to documentation 0 is not valid index for stack. Also it is
stated, that if some function gets invalid index then it should return
indication of error.  In real life it is not so. And 0 index is exactly
the same as L->top.  Not that I'm really picky :) but that lead to the
bug that was not so easy to track, since I was assuming that
documentation is correct :)

You can see it for yourself in sources for lua-5.0. Everything is in
lapi.c.

In luaA_index (as well as in luaA_indexAcceptable)
if(idx > 0){}
else { negindex(L, idx); }

in negindex(L, idx)
  if (idx > LUA_REGISTRYINDEX) {
    api_check(L, idx != 0 && -idx <= L->top - L->base);
    return L->top+idx;
  }

And what does api_check? I could find only this
#define api_check(L, o)         /*{ assert(o); }*/
which means that for idx == 0 we get back L->top.

I guess something shall be fixed, either documentation, or the code.
Most likely the code, because as far as I understand L->top does not
contain valid pointers and that may lead to crashes or some other
strange stuff.

Probably in negindex by adding at the top

if(idx == 0) return NULL;

Andrei