[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua reference re-use
- From: Coda Highland <chighland@...>
- Date: Thu, 30 Jul 2015 10:11:08 -0700
On Thu, Jul 30, 2015 at 7:19 AM, Tim Perkins <firstname.lastname@example.org> wrote:
> I've also struggled with luaL_ref and luaL_unref in the past. So I will try
> to help.
> These functions create a sort of linked list of free indexes, always
> starting at the zeroth element of the table.
> I would recommend writing your own functions, to meet your specific needs.
> The original functions are not too complicated, and you can find the source
> in "lauxlib.c". In your case it should be pretty easy because you can just
> use a monotonically increasing integer.
> P.S. In my situation, I needed references but I also need to iterate over
> all the references. I asked (and answered) this question on stack overflow:
> On Thu, Jul 30, 2015 at 6:47 AM Chris Jones <email@example.com> wrote:
>> If one does luaL_ref(), luaL_unref() and luaL_ref() again, there's a fair
>> chance that the same reference number will be handed out twice.
>> I'm wondering if it would be better if the references handed out by
>> luaL_ref() were only ever given out once? I think it would help track down
>> use-after-free type bugs, where right now you get either confusing data of
>> the wrong type, or take incorrect action on data that happens to be the
>> right type.
>> Clearly this is only an issue in code that has bugs, but we know that all
>> code has bugs, and I think finding them would be easier if a lua_rawgeti()
>> on a reference that had been released, would be able to return a clear "NO
>> YOU ARE DOING SOMETHING WRONG" error.
Alternatively, you could write a replacement for luaL_unref that
doesn't actually unref at all, but instead replaces the object in that
registry slot with a sentinel with a metatable that throws highly
visible errors when you try to do anything to it.