[libc-commits] [libc] [libc] Add 'x' and 'e' mode support to fopen() (PR #224207)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Thu Sep 17 05:26:44 PDT 2026


================
@@ -215,7 +237,10 @@ int LinuxFile::reopen_unlocked(const char *path, const char *mode) {
     // Else the new file successfully opened, so we move it into the fd the old
     // file was using if the old fd exists.
     if (old_fd >= 0) {
-      auto dup_result = linux_syscalls::dup2(new_fd.value(), old_fd);
+      // Preserve close-on-exec atomically when replacing the old descriptor.
----------------
labath wrote:

This isn't "atomic" in the `!CLOEXEC` case, in the case that if the process execs between the `open` and `dup3` calls, then both new_fd and old_fd will get inherited. What would you say to adding something akin to `if (old_fd >=0) open_flags |= O_CLOEXEC;` around line 218? That way the new fd will not get inherited until we install it over the old FD.

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


More information about the libc-commits mailing list