[libc-commits] [libc] c3b2849 - [libc] Allow openat and creat to return fd 0. (#166466)

via libc-commits libc-commits at lists.llvm.org
Wed Nov 5 13:21:22 PST 2025


Author: Jackson Stogel
Date: 2025-11-05T13:21:18-08:00
New Revision: c3b284919139bc0758cd547a56ffd5dbf6e67bb1

URL: https://github.com/llvm/llvm-project/commit/c3b284919139bc0758cd547a56ffd5dbf6e67bb1
DIFF: https://github.com/llvm/llvm-project/commit/c3b284919139bc0758cd547a56ffd5dbf6e67bb1.diff

LOG: [libc] Allow openat and creat to return fd 0. (#166466)

Previously, if the `open` or `openat` syscalls returned 0 as a (valid)
file descriptor, the `creat` and `openat` wrappers would erroneously
return -1.

Added: 
    

Modified: 
    libc/src/fcntl/linux/creat.cpp
    libc/src/fcntl/linux/openat.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/fcntl/linux/creat.cpp b/libc/src/fcntl/linux/creat.cpp
index 71412a8e68c53..e74cef299b59f 100644
--- a/libc/src/fcntl/linux/creat.cpp
+++ b/libc/src/fcntl/linux/creat.cpp
@@ -27,11 +27,11 @@ LLVM_LIBC_FUNCTION(int, creat, (const char *path, int mode_flags)) {
       SYS_openat, AT_FDCWD, path, O_CREAT | O_WRONLY | O_TRUNC, mode_flags);
 #endif
 
-  if (fd > 0)
-    return fd;
-
-  libc_errno = -fd;
-  return -1;
+  if (fd < 0) {
+    libc_errno = -fd;
+    return -1;
+  }
+  return fd;
 }
 
 } // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/fcntl/linux/openat.cpp b/libc/src/fcntl/linux/openat.cpp
index b47ad1fb3bb0f..b80abe532e51c 100644
--- a/libc/src/fcntl/linux/openat.cpp
+++ b/libc/src/fcntl/linux/openat.cpp
@@ -32,11 +32,11 @@ LLVM_LIBC_FUNCTION(int, openat, (int dfd, const char *path, int flags, ...)) {
 
   int fd = LIBC_NAMESPACE::syscall_impl<int>(SYS_openat, dfd, path, flags,
                                              mode_flags);
-  if (fd > 0)
-    return fd;
-
-  libc_errno = -fd;
-  return -1;
+  if (fd < 0) {
+    libc_errno = -fd;
+    return -1;
+  }
+  return fd;
 }
 
 } // namespace LIBC_NAMESPACE_DECL


        


More information about the libc-commits mailing list