Skip to content
Snippets Groups Projects
Commit 88de7c34 authored by Alexander Turenko's avatar Alexander Turenko Committed by Vladimir Davydov
Browse files

Fix premature cdata collecting in luaT_pusherror()

This is follow up of 28c7e667 to fix
luaT_pusherror() itself, not only luaT_error().

Fixes #1955 (again).
parent f5c8b825
No related branches found
No related tags found
No related merge requests found
...@@ -75,12 +75,20 @@ luaL_error_gc(struct lua_State *L) ...@@ -75,12 +75,20 @@ luaL_error_gc(struct lua_State *L)
void void
luaT_pusherror(struct lua_State *L, struct error *e) luaT_pusherror(struct lua_State *L, struct error *e)
{ {
/*
* gh-1955 luaT_pusherror allocates Lua objects, thus it
* may trigger GC. GC may invoke finalizers which are
* arbitrary Lua code, potentially invalidating last error
* object, hence error_ref below.
*
* It also important to reference the error first and only
* then set the finalizer.
*/
error_ref(e);
assert(CTID_CONST_STRUCT_ERROR_REF != 0); assert(CTID_CONST_STRUCT_ERROR_REF != 0);
struct error **ptr = (struct error **) struct error **ptr = (struct error **)
luaL_pushcdata(L, CTID_CONST_STRUCT_ERROR_REF); luaL_pushcdata(L, CTID_CONST_STRUCT_ERROR_REF);
*ptr = e; *ptr = e;
/* The order is important - first reference the error, then set gc */
error_ref(e);
lua_pushcfunction(L, luaL_error_gc); lua_pushcfunction(L, luaL_error_gc);
luaL_setcdatagc(L, -2); luaL_setcdatagc(L, -2);
} }
...@@ -90,15 +98,7 @@ luaT_error(lua_State *L) ...@@ -90,15 +98,7 @@ luaT_error(lua_State *L)
{ {
struct error *e = diag_last_error(&fiber()->diag); struct error *e = diag_last_error(&fiber()->diag);
assert(e != NULL); assert(e != NULL);
/*
* gh-1955 luaT_pusherror allocates Lua objects, thus it may trigger
* GC. GC may invoke finalizers which are arbitrary Lua code,
* potentially invalidating last error object, hence error_ref
* below.
*/
error_ref(e);
luaT_pusherror(L, e); luaT_pusherror(L, e);
error_unref(e);
lua_error(L); lua_error(L);
unreachable(); unreachable();
return 0; return 0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment