lua-users home
lua-l archive

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


On Sat, Oct 22, 2011 at 7:55 AM, Patrick Rapin <toupie300@gmail.com> wrote:
> I just had a crash with Lua 5.2 and it took me a little to understand why.
> The problem is with the following instruction:
>
>  lua_getglobal(L, lua_tostring(L, -1));
>
> With Lua 5.1, this is no problem. The macro expands to
>
>  lua_getfield(L, LUA_GLOBALSINDEX,  lua_tostring(L, -1))
>
> But on Lua 5.2, the macro now expands to:
>
>  (lua_pushglobaltable(L), lua_getfield(L, -1, ( lua_tostring(L,
> -1))), lua_remove(L, -2))
>
> And when the lua_tostring is evaluated, the stack has changed !
>
> I propose to rewrite the macro with something like:
>
> #define lua_getglobal(L,s) \
>        do { const char* S=s; lua_pushglobaltable(L); lua_getfield(L, -1, S);
> lua_remove(L, -2); } while(0)

There is another problem here as well I think. Since lua_getglobal
pushes 2 elements on the stack instead of just one, we could have a
stack overflow. Unlike the recently brought up auxiliary library [1],
lua_* C API functions can't blindly use "extra" stack space?

[1] http://lua-users.org/lists/lua-l/2011-10/msg00909.html

-- 
- Patrick Donnelly