[libc-commits] [libc] [libc] Implement close_range in unistd (PR #236913)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Mon Nov 2 04:52:35 PST 2026


================
@@ -0,0 +1,106 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for close_range.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/fcntl_macros.h"
+#include "hdr/unistd_macros.h"
+#include "src/__support/CPP/scope.h"
+#include "src/fcntl/fcntl.h"
+#include "src/unistd/close.h"
+#include "src/unistd/close_range.h"
+#include "src/unistd/pipe.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcCloseRangeTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+namespace {
+
+bool create_adjacent_fds(int fds[2]) {
+  if (LIBC_NAMESPACE::pipe(fds) != 0)
+    return false;
+  if (fds[0] > fds[1]) {
+    int tmp = fds[0];
+    fds[0] = fds[1];
+    fds[1] = tmp;
+  }
----------------
labath wrote:

I think you can delete this. The loop should be correct even if the initial FDs are in the wrong order (which I've never seen happen as the kernel always assigns the lowest FD available).
```suggestion
```

https://github.com/llvm/llvm-project/pull/236913


More information about the libc-commits mailing list