lua-users home
lua-l archive

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

On Wed, Aug 31, 2011 at 2:15 PM, oliver <> wrote:
Checkstack is only useful when you push stuff onto the stack. But how do you validate, say, an index you give to a C function that queries the Lua stack? For instance: 

int someFunc(int stackIndex) {
    return lua_tonumber(L, stackIndex); 

The lua_tonumber does not check that index is acceptable; if it is not, it can return garbage (0, NaN, whatever) and the caller wouldn't know that app state is now undefined. 

The only solution is to assert that stackIndex != 0 and abs(stackIndex) <= top, or?


It helps a LOT to treat the stack as a stack! If you organise your code to expect arguments on the top of the stack (using negative indexes given to the lua_...() functions), then you do not need to know anything about random stack positions.

Here, lua_gettop() may be used to check that you do in fact have at least as many elements on the top as you expect.