[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A question about lua_toclose()
- From: Roberto Ierusalimschy <roberto@...>
- Date: Fri, 4 Sep 2020 11:18:03 -0300
> >>> What do you mean by "safe"? What is exactly your concerns?
> >>
> >> I mean, if the C stack unwind before calling __close when raising an
> >> error, the pointer is unsafe. because it's on the C stack.
> >
> [...]
>
> int
> foobar(lua_State *L) {
> struct mystruct foo;
> lua_settop(L, 0);
>
> lua_newtable(L); // index 1 <—— CLOSED
> lua_pushlightuserdata(L, &foo);
> [...]
>
> Lua calls CLOSED.__close() after foobar() returns, but CLOSED._ptr is the address on the C stackframe of the foobar(). It fells like:
>
> [...]
That is right! Sorry about my wrong answer. I did not realize that 'foo'
was a stack-allocated variable, not a pointer.
As Francisco pointed out, that code "smells fishy" :-) It is
always slippery to put pointers to stack-allocated variables into
heap-allocated ones. It may be worth adding a note in the manual about
this interaction between lua_toclose and stack-allocated variables.
-- Roberto