lua-users home
lua-l archive

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


On Fri, Apr 30, 2004 at 12:30:08PM -0300, Roberto Ierusalimschy wrote:
> > Also it is stated, that if some function gets invalid index then it
> > should return indication of error.
> 
> Are you sure? If the documentation says that, we really should correct
> the documentation.

Oh, I must admit there's no explicit statement. But I made my conclusion
from words

~~~~~~~~ Quote from section 3.2 ~~~~~~~~~~

Most query functions accept as indices any value inside the available
stack space, that is, indices up to the maximum stack size you have set
through lua_checkstack. Such indices are called acceptable indices. More
formally, we define an acceptable index as follows:

     (index < 0 && abs(index) <= top) || (index > 0 && index <= stackspace)

Note that 0 is never an acceptable index.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Which implies that query functions don't accept invalid indices, (which
in turn implies they reject them :) From which follows that 0 shall be
rejected somehow. :)

Next thing that really hit me was this

~~~~~~~~~~~ Quote from section 3.4 ~~~~~~~~~~~ 

lua_type returns the type of a value in the stack, or LUA_TNONE for a
non-valid index (that is, if that stack position is "empty").

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This function called with index 0 does not return LUA_TNONE. Well. It
returns value of whatever was stored in L->top. That could be function,
or table or whatever other junk.

Well, I understand that the code does not have to check if the index is
valid. After all the idea of GIGO (garbage in garbage out) is very close
for me :) But, I guess, I was looking for consistency. Look for example at
the code for luaA_indexAcceptable. It returns NULL if index is beyond
the top of the stack (if (o >= L->top) return NULL;) But for 0 it passes
control to negindex, which does not return NULL. So, some invalid
indexes are rejected, others are treated as valid. That's not
consistent.

Of course, if adding additional check for index being 0 is against
certain policy, or is not compatible with older releases, then just
forget about my question. I can always fix things in my version of code
:)

While we are talking about that, can someone tell me how can I find out
value of "stackspace" that is used in check for validity of index?
(formula above)

Thank you

Andrei