lua-users home
lua-l archive

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


On Tue, Feb 7, 2012 at 1:21 PM, HyperHacker <hyperhacker@gmail.com> wrote:
> On Tue, Feb 7, 2012 at 02:19, Rebel Neurofog <rebelneurofog@gmail.com> wrote:
>> On Tue, Feb 7, 2012 at 1:15 PM, John Tan <johnmave126@gmail.com> wrote:
>>> The following codes could perform the bug
>>> //C Source Code
>>> #include "lua.hpp"
>>> int f1(lua_State *L) {
>>> printf("%d\n", lua_tonumber(L, 1));
>>> return 0;
>>> }
>>> int main (int argc, char* argv[]) {
>>> lua_State *L = luaL_newstate();
>>> luaL_openlibs(L);
>>> lua_pushcfunction(L, f1);
>>> lua_setglobal(L, "f1");
>>> lua_getglobal(L, "f1");
>>> lua_pushnumber(L, 1);
>>> lua_call(L, 1, 0);
>>> return 0;
>>> }
>>>
>>> After executing it,f1 will print 0 on the screen which indicates that it
>>> gets nil on stack when it is called.
>>> I don't know if there is anybody else meeting the same problem...
>>> Hope to know what has happened...THX
>>
>> Try printf("%d\n", (int) lua_tonumber(L, 1));
>>
>
> Or better, use lua_tointeger() instead of lua_tonumber(), since the
> latter returns a double.
>
> --
> Sent from my toaster.
>

Why?
lua_tointeger () returns lua_Integer which is ptrdiff_t by default.
You'll still have to cast to int.
Although this will work out on little endian machines.
And the bug will remain unnoticed.