From 4d2e3de94ca71a98382b5c2d173c67a84357f677 Mon Sep 17 00:00:00 2001 From: sin-ack Date: Tue, 18 Jan 2022 13:09:59 +0000 Subject: [PATCH] LibCore: Don't manually close the fd when connection fails in sockets This is wrong because we have already set the fd in the PosixSocketHelper, and the destructor of the respective Socket class will close the fd for us. With the manual closing of the fd, we attempt to close the same fd twice which results in a crash. Thanks to stelar7 for noticing this bug. --- Userland/Libraries/LibCore/Stream.cpp | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/Userland/Libraries/LibCore/Stream.cpp b/Userland/Libraries/LibCore/Stream.cpp index fcc5800614..147e02a902 100644 --- a/Userland/Libraries/LibCore/Stream.cpp +++ b/Userland/Libraries/LibCore/Stream.cpp @@ -459,11 +459,7 @@ ErrorOr> TCPSocket::connect(SocketAddress const& addres auto fd = TRY(create_fd(SocketDomain::Inet, SocketType::Stream)); socket->m_helper.set_fd(fd); - auto result = connect_inet(fd, address); - if (result.is_error()) { - ::close(fd); - return result.release_error(); - } + TRY(connect_inet(fd, address)); socket->setup_notifier(); return socket; @@ -509,11 +505,7 @@ ErrorOr> UDPSocket::connect(SocketAddress const& addres auto fd = TRY(create_fd(SocketDomain::Inet, SocketType::Datagram)); socket->m_helper.set_fd(fd); - auto result = connect_inet(fd, address); - if (result.is_error()) { - ::close(fd); - return result.release_error(); - } + TRY(connect_inet(fd, address)); socket->setup_notifier(); return socket; @@ -526,11 +518,7 @@ ErrorOr> LocalSocket::connect(String const& path) auto fd = TRY(create_fd(SocketDomain::Local, SocketType::Stream)); socket->m_helper.set_fd(fd); - auto result = connect_local(fd, path); - if (result.is_error()) { - ::close(fd); - return result.release_error(); - } + TRY(connect_local(fd, path)); socket->setup_notifier(); return socket;