lua-users home
lua-l archive

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

I have noticed a bug in my Lua 4.0 interpreter. It seems to me that if I
push a userdata/tag pair using lua_pushusertag(), and the void * I pass in
is NULL, the value is mangled when I attempt to retrieve it.

Here is my belief about the problem. In lstring.c luaS_newudata() has the
ability to allocate additional memory for userdata created with
lua_newuserdata() (as opposed to lua_pushusertag()). It decides whether to
do this based on whether the incoming void* pointer is NULL, on line 136:

  ts->u.d.value = (udata == NULL) ? uts+1 : udata;

However, in the case I care about, no additional memory has been allocated
because the passed-in size_t is 0, so uts+1 points into nowhere special.
Further, uts+1 is the value I will get back from Lua (instead of NULL). Ugh.

So, my proposed fix is as follows:

  ts->u.d.value = (udata == NULL && s > 0) ? uts+1 : udata;

this seems to work just fine for me, but if there is some reason why this
fix will cause more harm than good, please let me know.