Problem: inconsistent error handling and unnecessary code duplication

Solution: make error handling consistent and use retired_fd to remove code duplication
This commit is contained in:
Simon Giesecke 2018-05-23 10:10:32 +02:00
parent d02ba13576
commit c432aada84
4 changed files with 18 additions and 36 deletions

View File

@ -76,14 +76,13 @@ zmq::fd_t zmq::open_socket (int domain_, int type_, int protocol_)
type_ |= SOCK_CLOEXEC;
#endif
fd_t s = socket (domain_, type_, protocol_);
const fd_t s = socket (domain_, type_, protocol_);
if (s == retired_fd) {
#ifdef ZMQ_HAVE_WINDOWS
if (s == INVALID_SOCKET)
return INVALID_SOCKET;
#else
if (s == -1)
return -1;
errno = wsa_error_to_errno (WSAGetLastError ());
#endif
return retired_fd;
}
// If there's no SOCK_CLOEXEC, let's try the second best option. Note that
// race condition can cause socket not to be closed (if fork happens

View File

@ -309,13 +309,8 @@ int zmq::socks_connecter_t::connect_to_proxy ()
// Create the socket.
s = open_socket (tcp_addr->family (), SOCK_STREAM, IPPROTO_TCP);
#ifdef ZMQ_HAVE_WINDOWS
if (s == INVALID_SOCKET)
if (s == retired_fd)
return -1;
#else
if (s == -1)
return -1;
#endif
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
// Switch it on in such cases.

View File

@ -279,15 +279,9 @@ int zmq::tcp_connecter_t::open ()
s = open_socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
#ifdef ZMQ_HAVE_WINDOWS
if (s == INVALID_SOCKET) {
errno = wsa_error_to_errno (WSAGetLastError ());
if (s == retired_fd) {
return -1;
}
#else
if (s == -1)
return -1;
#endif
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
// Switch it on in such cases.

View File

@ -195,20 +195,15 @@ int zmq::tcp_listener_t::set_address (const char *addr_)
s = open_socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
#ifdef ZMQ_HAVE_WINDOWS
if (s == INVALID_SOCKET) {
errno = wsa_error_to_errno (WSAGetLastError ());
if (s == retired_fd) {
return -1;
}
#if !defined _WIN32_WCE && !defined ZMQ_HAVE_WINDOWS_UWP
#if defined ZMQ_HAVE_WINDOWS && !defined _WIN32_WCE \
&& !defined ZMQ_HAVE_WINDOWS_UWP
// On Windows, preventing sockets to be inherited by child processes.
BOOL brc = SetHandleInformation ((HANDLE) s, HANDLE_FLAG_INHERIT, 0);
win_assert (brc);
#endif
#else
if (s == -1)
return -1;
#endif
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
// Switch it on in such cases.
@ -306,26 +301,25 @@ zmq::fd_t zmq::tcp_listener_t::accept ()
::accept (s, reinterpret_cast<struct sockaddr *> (&ss), &ss_len);
#endif
if (sock == retired_fd) {
#ifdef ZMQ_HAVE_WINDOWS
if (sock == INVALID_SOCKET) {
const int last_error = WSAGetLastError ();
wsa_assert (last_error == WSAEWOULDBLOCK || last_error == WSAECONNRESET
|| last_error == WSAEMFILE || last_error == WSAENOBUFS);
return retired_fd;
}
#if !defined _WIN32_WCE && !defined ZMQ_HAVE_WINDOWS_UWP
// On Windows, preventing sockets to be inherited by child processes.
BOOL brc = SetHandleInformation ((HANDLE) sock, HANDLE_FLAG_INHERIT, 0);
win_assert (brc);
#endif
#else
if (sock == -1) {
errno_assert (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR
|| errno == ECONNABORTED || errno == EPROTO
|| errno == ENOBUFS || errno == ENOMEM || errno == EMFILE
|| errno == ENFILE);
#endif
return retired_fd;
}
#if defined ZMQ_HAVE_WINDOWS && !defined _WIN32_WCE \
&& !defined ZMQ_HAVE_WINDOWS_UWP
// On Windows, preventing sockets to be inherited by child processes.
BOOL brc = SetHandleInformation ((HANDLE) sock, HANDLE_FLAG_INHERIT, 0);
win_assert (brc);
#endif
#if (!defined ZMQ_HAVE_SOCK_CLOEXEC || !defined HAVE_ACCEPT4) \