Index: runtime/bin/socket_android.cc |
diff --git a/runtime/bin/socket_android.cc b/runtime/bin/socket_android.cc |
index ae83b47392a1f128b815be6e730054dbe8fd9310..07e6894e6a8747995b3e17a35eec23b1a53436b4 100644 |
--- a/runtime/bin/socket_android.cc |
+++ b/runtime/bin/socket_android.cc |
@@ -25,6 +25,13 @@ |
namespace dart { |
namespace bin { |
+static void SaveErrorAndClose(intptr_t fd) { |
+ int err = errno; |
+ VOID_TEMP_FAILURE_RETRY(close(fd)); |
+ errno = err; |
+} |
+ |
+ |
SocketAddress::SocketAddress(struct sockaddr* sa) { |
ASSERT(INET6_ADDRSTRLEN >= INET_ADDRSTRLEN); |
if (!Socket::FormatNumericAddress(*reinterpret_cast<RawAddr*>(sa), as_string_, |
@@ -55,7 +62,10 @@ static intptr_t Create(const RawAddr& addr) { |
if (fd < 0) { |
return -1; |
} |
- FDUtils::SetCloseOnExec(fd); |
+ if (!FDUtils::SetCloseOnExec(fd)) { |
+ SaveErrorAndClose(fd); |
+ return -1; |
+ } |
return fd; |
} |
@@ -66,7 +76,7 @@ static intptr_t Connect(intptr_t fd, const RawAddr& addr) { |
if ((result == 0) || (errno == EINPROGRESS)) { |
return fd; |
} |
- VOID_TEMP_FAILURE_RETRY(close(fd)); |
+ SaveErrorAndClose(fd); |
return -1; |
} |
@@ -77,8 +87,10 @@ intptr_t Socket::CreateConnect(const RawAddr& addr) { |
return fd; |
} |
- FDUtils::SetNonBlocking(fd); |
- |
+ if (!FDUtils::SetNonBlocking(fd)) { |
+ SaveErrorAndClose(fd); |
+ return -1; |
+ } |
return Connect(fd, addr); |
} |
@@ -93,7 +105,7 @@ intptr_t Socket::CreateBindConnect(const RawAddr& addr, |
intptr_t result = TEMP_FAILURE_RETRY( |
bind(fd, &source_addr.addr, SocketAddress::GetAddrLength(source_addr))); |
if ((result != 0) && (errno != EINPROGRESS)) { |
- VOID_TEMP_FAILURE_RETRY(close(fd)); |
+ SaveErrorAndClose(fd); |
return -1; |
} |
@@ -310,7 +322,10 @@ intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { |
return -1; |
} |
- FDUtils::SetCloseOnExec(fd); |
+ if (!FDUtils::SetCloseOnExec(fd)) { |
+ SaveErrorAndClose(fd); |
+ return -1; |
+ } |
if (reuseAddress) { |
int optval = 1; |
@@ -320,11 +335,14 @@ intptr_t Socket::CreateBindDatagram(const RawAddr& addr, bool reuseAddress) { |
if (NO_RETRY_EXPECTED( |
bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
- VOID_TEMP_FAILURE_RETRY(close(fd)); |
+ SaveErrorAndClose(fd); |
return -1; |
} |
- FDUtils::SetNonBlocking(fd); |
+ if (!FDUtils::SetNonBlocking(fd)) { |
+ SaveErrorAndClose(fd); |
+ return -1; |
+ } |
return fd; |
} |
@@ -358,7 +376,10 @@ intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
return -1; |
} |
- FDUtils::SetCloseOnExec(fd); |
+ if (!FDUtils::SetCloseOnExec(fd)) { |
+ SaveErrorAndClose(fd); |
+ return -1; |
+ } |
int optval = 1; |
VOID_NO_RETRY_EXPECTED( |
@@ -372,7 +393,7 @@ intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
if (NO_RETRY_EXPECTED( |
bind(fd, &addr.addr, SocketAddress::GetAddrLength(addr))) < 0) { |
- VOID_TEMP_FAILURE_RETRY(close(fd)); |
+ SaveErrorAndClose(fd); |
return -1; |
} |
@@ -382,18 +403,19 @@ intptr_t ServerSocket::CreateBindListen(const RawAddr& addr, |
// Don't close the socket until we have created a new socket, ensuring |
// that we do not get the bad port number again. |
intptr_t new_fd = CreateBindListen(addr, backlog, v6_only); |
- int err = errno; |
- VOID_TEMP_FAILURE_RETRY(close(fd)); |
- errno = err; |
+ SaveErrorAndClose(fd); |
return new_fd; |
} |
if (NO_RETRY_EXPECTED(listen(fd, backlog > 0 ? backlog : SOMAXCONN)) != 0) { |
- VOID_TEMP_FAILURE_RETRY(close(fd)); |
+ SaveErrorAndClose(fd); |
return -1; |
} |
- FDUtils::SetNonBlocking(fd); |
+ if (!FDUtils::SetNonBlocking(fd)) { |
+ SaveErrorAndClose(fd); |
+ return -1; |
+ } |
return fd; |
} |
@@ -428,8 +450,14 @@ intptr_t ServerSocket::Accept(intptr_t fd) { |
socket = kTemporaryFailure; |
} |
} else { |
- FDUtils::SetCloseOnExec(socket); |
- FDUtils::SetNonBlocking(socket); |
+ if (!FDUtils::SetCloseOnExec(socket)) { |
+ SaveErrorAndClose(socket); |
+ return -1; |
+ } |
+ if (!FDUtils::SetNonBlocking(socket)) { |
+ SaveErrorAndClose(socket); |
+ return -1; |
+ } |
} |
return socket; |
} |