lua-users home
lua-l archive

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


Hi,

the standard practice of using the address of a static const
object as a unique lightuserdata key is problematic:

static const int regkey_foo = 1;
static const int regkey_bar = 1;
...
lua_pushlightuserdata(L, (void *)&regkey_foo);
lua_pushlightuserdata(L, (void *)&regkey_bar);

PIL 27.3 describes this technique as "bulletproof" to get a
unique address. Alas, it's not.

The problem is that the compiler/linker may decide to join
constants with identical _values_. Unfortunately this makes the
_addresses_ the same, too. This in turn means the lightuserdata
keys are the same and happily overwrite each other in the
registry. :-(

Note that the linker may even decide to join constants from
different source files, linked to the same executable or shared
library.

[This is not a theory: LuaJIT 1.1.1 was broken on Mac OS X due to
this issue (GCC 4.0 + Mach linker).]

Solution:
- Ensure the const _values_ are unique across your whole program.
- Or use the addresses of non-const static variables.

Better check your programs now -- this is very hard to diagnose.

Bye,
     Mike