aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSlava Gurevich <sgurevich@gmail.com>2022-08-05 13:54:08 -0700
committerSlava Gurevich <sgurevich@gmail.com>2022-08-06 23:06:55 -0700
commitaa4977f2e1354bb5e1937235dd12199839ab0801 (patch)
treedb33b6ba2cf13b1c30eed422d379fbee8a22f2c2
parent5e9b6a2243199d19fb7f23f8a3dce5ac813a2151 (diff)
[LLDB][NFC] Reliability fixes to TCPSocket code
Patch the following issues found by static code inspection: - Unchecked return values from lib calls - Passing potentially negative arg into a function that requires non-negative input - Possible socket double-close Differential Revision: https://reviews.llvm.org/D131294
-rw-r--r--lldb/source/Host/common/TCPSocket.cpp23
1 files changed, 16 insertions, 7 deletions
diff --git a/lldb/source/Host/common/TCPSocket.cpp b/lldb/source/Host/common/TCPSocket.cpp
index eabc9fef8a66..f424b42db7b6 100644
--- a/lldb/source/Host/common/TCPSocket.cpp
+++ b/lldb/source/Host/common/TCPSocket.cpp
@@ -174,7 +174,10 @@ Status TCPSocket::Connect(llvm::StringRef name) {
continue;
}
- SetOptionNoDelay();
+ if (-1 == SetOptionNoDelay()) {
+ Close();
+ continue;
+ }
error.Clear();
return error;
@@ -200,15 +203,18 @@ Status TCPSocket::Listen(llvm::StringRef name, int backlog) {
for (SocketAddress &address : addresses) {
int fd = Socket::CreateSocket(address.GetFamily(), kType, IPPROTO_TCP,
m_child_processes_inherit, error);
- if (error.Fail())
+ if (error.Fail() || fd < 0)
continue;
// enable local address reuse
int option_value = 1;
set_socket_option_arg_type option_value_p =
reinterpret_cast<set_socket_option_arg_type>(&option_value);
- ::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, option_value_p,
- sizeof(option_value));
+ if (-1 == ::setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, option_value_p,
+ sizeof(option_value))) {
+ CLOSE_SOCKET(fd);
+ continue;
+ }
SocketAddress listen_address = address;
if(!listen_address.IsLocalhost())
@@ -255,8 +261,8 @@ Status TCPSocket::Accept(Socket *&conn_socket) {
return error;
}
- int sock = -1;
- int listen_sock = -1;
+ int sock = kInvalidSocketValue;
+ int listen_sock = kInvalidSocketValue;
lldb_private::SocketAddress AcceptAddr;
MainLoop accept_loop;
std::vector<MainLoopBase::ReadHandleUP> handles;
@@ -288,7 +294,10 @@ Status TCPSocket::Accept(Socket *&conn_socket) {
lldb_private::SocketAddress &AddrIn = m_listen_sockets[listen_sock];
if (!AddrIn.IsAnyAddr() && AcceptAddr != AddrIn) {
- CLOSE_SOCKET(sock);
+ if (kInvalidSocketValue != sock) {
+ CLOSE_SOCKET(sock);
+ sock = kInvalidSocketValue;
+ }
llvm::errs() << llvm::formatv(
"error: rejecting incoming connection from {0} (expecting {1})",
AcceptAddr.GetIPAddress(), AddrIn.GetIPAddress());