diff --git a/src/afd.c b/src/afd.c index a2f9d24..425ec1c 100644 --- a/src/afd.c +++ b/src/afd.c @@ -116,7 +116,7 @@ static ssize_t _afd_get_protocol_info(SOCKET socket, SO_PROTOCOL_INFOW, (char*) protocol_info, &opt_len) != 0) - return_handle_error(-1, socket); + return_error(-1); id = -1; for (size_t i = 0; i < array_count(AFD_PROVIDER_GUID_LIST); i++) { diff --git a/src/api.c b/src/api.c index 10438de..25e8d20 100644 --- a/src/api.c +++ b/src/api.c @@ -79,22 +79,34 @@ int epoll_close(HANDLE ephnd) { int epoll_ctl(HANDLE ephnd, int op, SOCKET sock, struct epoll_event* ev) { reflock_tree_node_t* tree_node; ep_port_t* port_info; - int result; + int r; if (init() < 0) return -1; tree_node = reflock_tree_find_and_ref(&_epoll_handle_tree, (uintptr_t) ephnd); - if (tree_node == NULL) - return_handle_error(-1, ephnd, ERROR_INVALID_PARAMETER); - port_info = _handle_tree_node_to_port(tree_node); + if (tree_node == NULL) { + err_set_win_error(ERROR_INVALID_PARAMETER); + goto err; + } - result = ep_port_ctl(port_info, op, sock, ev); + port_info = _handle_tree_node_to_port(tree_node); + r = ep_port_ctl(port_info, op, sock, ev); reflock_tree_node_unref(tree_node); - return result; + if (r < 0) + goto err; + + return 0; + +err: + /* On Linux, in the case of epoll_ctl_mod(), EBADF takes precendence over + * other errors. Wepoll copies this behavior. */ + err_check_handle(ephnd); + err_check_handle((HANDLE) sock); + return -1; } int epoll_wait(HANDLE ephnd, diff --git a/src/port.c b/src/port.c index f0631b7..4505321 100644 --- a/src/port.c +++ b/src/port.c @@ -320,7 +320,7 @@ static int _ep_port_ctl_op(ep_port_t* port_info, case EPOLL_CTL_DEL: return _ep_port_ctl_del(port_info, sock); default: - return_handle_error(-1, sock, ERROR_INVALID_PARAMETER); + return_error(-1, ERROR_INVALID_PARAMETER); } } @@ -355,7 +355,7 @@ ep_sock_t* ep_port_find_socket(ep_port_t* port_info, SOCKET socket) { ep_sock_t* sock_info = safe_container_of( tree_find(&port_info->sock_tree, socket), ep_sock_t, tree_node); if (sock_info == NULL) - return_handle_error(NULL, socket, ERROR_NOT_FOUND); + return_error(NULL, ERROR_NOT_FOUND); return sock_info; } diff --git a/test/test-error.c b/test/test-error.c index b15cff9..f2f95c9 100644 --- a/test/test-error.c +++ b/test/test-error.c @@ -101,7 +101,8 @@ int main(void) { check_error(r < 0, EBADF, ERROR_INVALID_HANDLE); r = epoll_ctl(valid_ephnd, -1, INVALID_SOCKET, &ev); check_error(r < 0, EBADF, ERROR_INVALID_HANDLE); - /* TODO: bad `ephnd` type with invalid `sock`. */ + r = epoll_ctl(bad_type, -1, INVALID_SOCKET, &ev); + check_error(r < 0, EBADF, ERROR_INVALID_HANDLE); r = closesocket(sock_valid); check(r == 0);