Truly independent web browser
LibIPC: Close a File's descriptor when another is moved into it
Shannon Boothbd6c48048a6f
diff --git a/Libraries/LibIPC/File.cpp b/Libraries/LibIPC/File.cppindex 04581416ec4..35891ebd041 100644--- a/Libraries/LibIPC/File.cpp+++ b/Libraries/LibIPC/File.cpp@@ -43,6 +43,8 @@ File::File(File&& other)File& File::operator=(File&& other){if (this != &other) {+ if (m_fd != -1)+ (void)Core::System::close(m_fd);m_fd = exchange(other.m_fd, -1);}return *this;diff --git a/Tests/LibIPC/CMakeLists.txt b/Tests/LibIPC/CMakeLists.txtindex 300647456cf..ca990951cd0 100644--- a/Tests/LibIPC/CMakeLists.txt+++ b/Tests/LibIPC/CMakeLists.txt@@ -5,6 +5,9 @@ set(TEST_SOURCESTestDecoder.cpp)+if (NOT WIN32)+ list(APPEND TEST_SOURCES TestFile.cpp)+endif()if (UNIX AND NOT APPLE)list(APPEND TEST_SOURCES TestTransportSocket.cpp)endif()diff --git a/Tests/LibIPC/TestFile.cpp b/Tests/LibIPC/TestFile.cppnew file mode 100644index 00000000000..5cde341fbc1--- /dev/null+++ b/Tests/LibIPC/TestFile.cpp@@ -0,0 +1,27 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibCore/System.h>+#include <LibIPC/File.h>+#include <LibTest/TestCase.h>+#include <fcntl.h>++static bool is_open(int fd)+{+ return fcntl(fd, F_GETFD) != -1;+}++TEST_CASE(move_assignment_closes_the_descriptor_it_replaces)+{+ auto replaced_fd = TRY_OR_FAIL(Core::System::open("/dev/null"sv, O_RDONLY | O_CLOEXEC));+ auto moved_fd = TRY_OR_FAIL(Core::System::open("/dev/null"sv, O_RDONLY | O_CLOEXEC));++ auto file = IPC::File::adopt_fd(replaced_fd);+ file = IPC::File::adopt_fd(moved_fd);+ EXPECT(!is_open(replaced_fd));+ EXPECT(is_open(moved_fd));+ EXPECT_EQ(file.fd(), moved_fd);+}