Skip to content

Commit

Permalink
Don't leak callbacks when uv functions return error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Apr 26, 2016
1 parent 97984a8 commit 36d26e3
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ static int luv_getaddrinfo(lua_State* L) {
else service = luaL_checkstring(L, 2);
if (!lua_isnoneornil(L, 3)) luaL_checktype(L, 3, LUA_TTABLE);
else hints = NULL;
ref = lua_isnoneornil(L, 4) ? LUA_NOREF : luv_check_continuation(L, 4);
if (hints) {
// Initialize the hints
memset(hints, 0, sizeof(*hints));
Expand Down Expand Up @@ -182,11 +181,13 @@ static int luv_getaddrinfo(lua_State* L) {
lua_pop(L, 1);
}

ref = luv_check_continuation(L, 4);
req = lua_newuserdata(L, sizeof(*req));
req->data = luv_setup_req(L, ref);

ret = uv_getaddrinfo(luv_loop(L), req, ref == LUA_NOREF ? NULL : luv_getaddrinfo_cb, node, service, hints);
if (ret < 0) {
luv_cleanup_req(L, req->data);
lua_pop(L, 1);
return luv_error(L, ret);
}
Expand Down Expand Up @@ -274,13 +275,14 @@ static int luv_getnameinfo(lua_State* L) {
}
lua_pop(L, 1);

ref = lua_isnoneornil(L, 2) ? LUA_NOREF : luv_check_continuation(L, 2);
ref = luv_check_continuation(L, 2);

req = lua_newuserdata(L, sizeof(*req));
req->data = luv_setup_req(L, ref);

ret = uv_getnameinfo(luv_loop(L), req, ref == LUA_NOREF ? NULL : luv_getnameinfo_cb, (struct sockaddr*)&addr, flags);
if (ret < 0) {
luv_cleanup_req(L, req->data);
lua_pop(L, 1);
return luv_error(L, ret);
}
Expand All @@ -293,4 +295,3 @@ static int luv_getnameinfo(lua_State* L) {
}
return 1;
}

4 changes: 3 additions & 1 deletion src/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ static int luv_shutdown(lua_State* L) {
req->data = luv_setup_req(L, ref);
ret = uv_shutdown(req, handle, luv_shutdown_cb);
if (ret < 0) {
luv_cleanup_req(L, req->data);
lua_pop(L, 1);
return luv_error(L, ret);
}
Expand Down Expand Up @@ -176,6 +177,7 @@ static int luv_write(lua_State* L) {
return luaL_argerror(L, 2, "data must be string or table of strings");
}
if (ret < 0) {
luv_cleanup_req(L, req->data);
lua_pop(L, 1);
return luv_error(L, ret);
}
Expand Down Expand Up @@ -208,6 +210,7 @@ static int luv_write2(lua_State* L) {
return luaL_argerror(L, 2, "data must be string or table of strings");
}
if (ret < 0) {
luv_cleanup_req(L, req->data);
lua_pop(L, 1);
return luv_error(L, ret);
}
Expand Down Expand Up @@ -260,4 +263,3 @@ static int luv_stream_set_blocking(lua_State* L) {
lua_pushinteger(L, ret);
return 1;
}

1 change: 1 addition & 0 deletions src/tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ static int luv_tcp_connect(lua_State* L) {
req->data = luv_setup_req(L, ref);
ret = uv_tcp_connect(req, handle, (struct sockaddr*)&addr, luv_connect_cb);
if (ret < 0) {
luv_cleanup_req(L, req->data);
lua_pop(L, 1);
return luv_error(L, ret);
}
Expand Down
1 change: 1 addition & 0 deletions src/udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ static int luv_udp_send(lua_State* L) {
req->data = luv_setup_req(L, ref);
ret = uv_udp_send(req, handle, &buf, 1, (struct sockaddr*)&addr, luv_udp_send_cb);
if (ret < 0) {
luv_cleanup_req(L, req->data);
lua_pop(L, 1);
return luv_error(L, ret);
}
Expand Down

0 comments on commit 36d26e3

Please sign in to comment.