[libc-commits] [libc] [libc] prefer *at syscalls in sys/stat wrappers (PR #201051)

via libc-commits libc-commits at lists.llvm.org
Tue Jun 2 00:58:42 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Kiriti Ponduri  (udaykiriti)

<details>
<summary>Changes</summary>

        - These changes flips the #ifdef order to prefer the *at syscalls over normal ones.
	- In modern architectures, *at system calls are preferred over normal system calls cuz of safety issues.
	- So by checking for ""*at"" system calls first, we ensure better compatibility with modern systems.
	- After then normal syscalls moved else or elif for support to older ones.
	- From merged pr(#<!-- -->195792) .
	- issue(#<!-- -->195620).

---
Full diff: https://github.com/llvm/llvm-project/pull/201051.diff


2 Files Affected:

- (modified) libc/src/__support/File/linux/dir.cpp (+4-4) 
- (modified) libc/src/__support/File/linux/file.cpp (+4-4) 


``````````diff
diff --git a/libc/src/__support/File/linux/dir.cpp b/libc/src/__support/File/linux/dir.cpp
index 5fe44fa8297b6..03203ae208de4 100644
--- a/libc/src/__support/File/linux/dir.cpp
+++ b/libc/src/__support/File/linux/dir.cpp
@@ -13,17 +13,17 @@
 #include "src/__support/macros/config.h"
 
 #include "hdr/fcntl_macros.h" // For open flags
-#include <sys/syscall.h> // For syscall numbers
+#include <sys/syscall.h>      // For syscall numbers
 
 namespace LIBC_NAMESPACE_DECL {
 
 ErrorOr<int> platform_opendir(const char *name) {
   int open_flags = O_RDONLY | O_DIRECTORY | O_CLOEXEC;
-#ifdef SYS_open
-  int fd = LIBC_NAMESPACE::syscall_impl<int>(SYS_open, name, open_flags);
-#elif defined(SYS_openat)
+#ifdef SYS_openat
   int fd =
       LIBC_NAMESPACE::syscall_impl<int>(SYS_openat, AT_FDCWD, name, open_flags);
+#elif defined(SYS_open)
+  int fd = LIBC_NAMESPACE::syscall_impl<int>(SYS_open, name, open_flags);
 #else
 #error                                                                         \
     "SYS_open and SYS_openat syscalls not available to perform an open operation."
diff --git a/libc/src/__support/File/linux/file.cpp b/libc/src/__support/File/linux/file.cpp
index 2bef96a102a0c..a044c11393bd2 100644
--- a/libc/src/__support/File/linux/file.cpp
+++ b/libc/src/__support/File/linux/file.cpp
@@ -92,12 +92,12 @@ ErrorOr<File *> openfile(const char *path, const char *mode) {
   constexpr long OPEN_MODE =
       S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
 
-#ifdef SYS_open
-  int fd =
-      LIBC_NAMESPACE::syscall_impl<int>(SYS_open, path, open_flags, OPEN_MODE);
-#elif defined(SYS_openat)
+#ifdef SYS_openat
   int fd = LIBC_NAMESPACE::syscall_impl<int>(SYS_openat, AT_FDCWD, path,
                                              open_flags, OPEN_MODE);
+#elif defined(SYS_open)
+  int fd =
+      LIBC_NAMESPACE::syscall_impl<int>(SYS_open, path, open_flags, OPEN_MODE);
 #else
 #error "open and openat syscalls not available."
 #endif

``````````

</details>


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


More information about the libc-commits mailing list