Skip to content

Commit c94ac1a

Browse files
authored
Merge pull request #21 from standardbots/fix/socket-leak-on-connect-failure
Fix file descriptor leak on connection failure
2 parents 17f7634 + 397a169 commit c94ac1a

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/TCP/connection.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,10 @@ Connection Connection::with(std::string addr, int port) {
204204
server.sin_port = ::htons(port);
205205
server.sin_addr = {inet_addr(addr.c_str())};
206206

207-
if (::connect(sock, reinterpret_cast<struct sockaddr *>(&server), sizeof(server)) < 0)
207+
if (::connect(sock, reinterpret_cast<struct sockaddr *>(&server), sizeof(server)) < 0) {
208+
::close(sock);
208209
throw std::runtime_error("Cannot connect, errno = " + std::to_string(errno));
210+
}
209211

210212
return Connection(sock);
211213
}

0 commit comments

Comments
 (0)