# HG changeset patch # User jbe # Date 1428626443 -7200 # Node ID d8469c1039e64ed0fabc68b43182eec9e6d08cf9 # Parent 2ab19e15aa6bd19859afbb77c70849cc9d75f23a Bugfixes regarding treatment of memory allocation errors and closing file descriptors diff -r 2ab19e15aa6b -r d8469c1039e6 moonbridge_io.c --- a/moonbridge_io.c Fri Apr 10 02:24:54 2015 +0200 +++ b/moonbridge_io.c Fri Apr 10 02:40:43 2015 +0200 @@ -769,6 +769,7 @@ if (strlen(path) > path_maxlen) luaL_error(L, "Path too long; only %i characters allowed", path_maxlen); strcpy(sockaddr.sun_path, path); listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t)); + listener->fd = -1; luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY); sock = socket( PF_LOCAL, @@ -810,6 +811,7 @@ host = luaL_optstring(L, 1, NULL); port = luaL_checkstring(L, 2); listener = lua_newuserdata(L, sizeof(moonbr_io_listener_t)); + listener->fd = -1; luaL_setmetatable(L, MOONBR_IO_LISTENER_MT_REGKEY); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; @@ -940,7 +942,7 @@ static int moonbr_io_listenergc(lua_State *L) { moonbr_io_listener_t *listener; listener = luaL_checkudata(L, 1, MOONBR_IO_LISTENER_MT_REGKEY); - if (listener->fd) close(listener->fd); + if (listener->fd >= 0) close(listener->fd); listener->fd = -1; return 0; }