[libc-commits] [libc] [libc] Implement freopen (PR #207837)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Wed Jul 8 07:50:49 PDT 2026


================
@@ -128,9 +135,9 @@ ErrorOr<LinuxFile *> create_file_from_fd(int fd, const char *mode) {
 
   using OpenMode = File::OpenMode;
   if (((fd_flags & O_ACCMODE) == O_RDONLY &&
-       !(modeflags & static_cast<ModeFlags>(OpenMode::READ))) ||
+       (modeflags & static_cast<ModeFlags>(OpenMode::WRITE))) ||
       ((fd_flags & O_ACCMODE) == O_WRONLY &&
-       !(modeflags & static_cast<ModeFlags>(OpenMode::WRITE)))) {
+       (modeflags & static_cast<ModeFlags>(OpenMode::READ)))) {
     return Error(EINVAL);
   }
----------------
kaladron wrote:

It looks like this doesn't catch r+ or w+/a+ violations.  Would something like this work?

```c++
  using OpenMode = File::OpenMode;
  using ModeFlags = File::ModeFlags;

  constexpr ModeFlags REQUIRES_WRITE = 
      static_cast<ModeFlags>(OpenMode::WRITE) | 
      static_cast<ModeFlags>(OpenMode::APPEND) | 
      static_cast<ModeFlags>(OpenMode::PLUS);

  constexpr ModeFlags REQUIRES_READ = 
      static_cast<ModeFlags>(OpenMode::READ) | 
      static_cast<ModeFlags>(OpenMode::PLUS);

  if (((fd_flags & O_ACCMODE) == O_RDONLY && (modeflags & REQUIRES_WRITE)) ||
      ((fd_flags & O_ACCMODE) == O_WRONLY && (modeflags & REQUIRES_READ))) {
    return Error(EINVAL);
  }
```



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


More information about the libc-commits mailing list