event_callback: Fix segmentation fault
authorKim Alvefur <zash@zash.se>
Thu, 25 Apr 2013 20:57:36 +0000 (25 22:57 +0200)
committerKim Alvefur <zash@zash.se>
Thu, 25 Apr 2013 21:15:16 +0000 (25 23:15 +0200)
If an event is closed and an error occurs in the same callback, it would
cause a segfault due to cb->base being NULL

src/event_callback.c

index 494fba2..f5ad7cb 100644 (file)
@@ -30,10 +30,12 @@ void luaevent_callback(int fd, short event, void* p) {
        L = cb->base->loop_L;
        lua_rawgeti(L, LUA_REGISTRYINDEX, cb->callbackRef);
        lua_pushinteger(L, event);
+       /* cb->base may be NULL after the pcall, if the event is destroyed */
+       le_base* base = cb->base;
        if(lua_pcall(L, 1, 2, 0))
        {
-               cb->base->errorMessage = luaL_ref(L, LUA_REGISTRYINDEX);
-               event_base_loopbreak(cb->base->base);
+               base->errorMessage = luaL_ref(L, LUA_REGISTRYINDEX);
+               event_base_loopbreak(base->base);
                lua_pop(L, 1);
                return;
        }