[libc-commits] [libc] [libc] Allow openat to return fd 0. (PR #166466)
Jackson Stogel via libc-commits
libc-commits at lists.llvm.org
Wed Nov 5 12:29:53 PST 2025
https://github.com/jtstogel updated https://github.com/llvm/llvm-project/pull/166466
>From ce5939a02eb0ab0171afc5893b2c1995bcfe64cf Mon Sep 17 00:00:00 2001
From: Jackson Stogel <jtstogel at gmail.com>
Date: Tue, 4 Nov 2025 22:53:09 +0000
Subject: [PATCH 1/2] Allow openat to return fd 0.
---
libc/src/fcntl/linux/openat.cpp | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
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
>From 98fbfdcbc291e9501600c4a418286cd5605eaa72 Mon Sep 17 00:00:00 2001
From: Jackson Stogel <jtstogel at gmail.com>
Date: Wed, 5 Nov 2025 20:29:24 +0000
Subject: [PATCH 2/2] Also allow creat to return a zero file descriptor.
---
libc/src/fcntl/linux/creat.cpp | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
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
More information about the libc-commits
mailing list