Segfault in luaL_ref into function

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Segfault in luaL_ref into function

Alexander Gladysh
Hi, list!

Lua 5.1.4, OS X 10.5.6

Perhaps luaL_ref() should check that given stack index points to a
table. I've accidentally pointed it to a function, and got a segfault:

static int Lref_in_function(lua_State * L)
{
  luaL_checktype(L, 1, LUA_TFUNCTION);
  lua_newtable(L);
  luaL_ref(L, -2);
  lua_pop(L, 1);

  return 0;
}

GDB output:

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> m = require 'misc'
Reading symbols for shared libraries . done
> m.ref_in_function(print)

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x458a0012
0x00010a67 in luaH_getnum ()
(gdb) bt
#0  0x00010a67 in luaH_getnum ()
#1  0x0000426a in lua_rawgeti ()
#2  0x0001564b in luaL_ref ()
#3  0x0004d9b2 in Lref_in_function (L=0x100180) at misc.c:39
#4  0x00008923 in luaD_precall ()
#5  0x00013e4e in luaV_execute ()
#6  0x00008d60 in luaD_call ()
#7  0x000047c1 in f_call ()
#8  0x0000823b in luaD_rawrunprotected ()
#9  0x00009082 in luaD_pcall ()
#10 0x00004835 in lua_pcall ()
#11 0x000026b3 in docall ()
#12 0x00002bd0 in dotty ()
#13 0x00002ec0 in pmain ()
#14 0x00008923 in luaD_precall ()
#15 0x00008d03 in luaD_call ()
#16 0x00004900 in f_Ccall ()
#17 0x0000823b in luaD_rawrunprotected ()
#18 0x00009082 in luaD_pcall ()
#19 0x00004957 in lua_cpcall ()
#20 0x00003235 in main ()
(gdb)

Alexander.

Reply | Threaded
Open this post in threaded view
|

Re: Segfault in luaL_ref into function

Luiz Henrique de Figueiredo
> Perhaps luaL_ref() should check that given stack index points to a
> table. I've accidentally pointed it to a function, and got a segfault:

There's no hand-holding in the C API.