Skip to content

fix: O_ACCMODE flag handling in fdopen/fopen/fread/fwrite/fcntl - #437

Merged
afxgroup merged 1 commit into
developmentfrom
fix/fdopen-fread-fwrite-accmode-flags
Jun 19, 2026
Merged

fix: O_ACCMODE flag handling in fdopen/fopen/fread/fwrite/fcntl#437
afxgroup merged 1 commit into
developmentfrom
fix/fdopen-fread-fwrite-accmode-flags

Conversation

@afxgroup

Copy link
Copy Markdown
Collaborator
  • fdopen/fopen: replace FLAG_IS_SET(open_mode, O_RDONLY) with O_ACCMODE mask comparison. FLAG_IS_SET with O_RDONLY=0 always returns true, causing IOBF_READ to be set on write-only streams.

  • fcntl F_GETFL: include O_RDONLY/O_WRONLY/O_RDWR and O_APPEND bits in the returned flags. Previously F_GETFL only returned O_NONBLOCK/ O_ASYNC/O_PATH, breaking the BFD-style 'fdflags & O_ACCMODE' switch.

  • fread: move cantread() check before the fast path. The fast path bypassed cantread when the buffer was already allocated, allowing reads on write-only streams to silently succeed.

  • fwrite: add cantwrite() check before the fast path, symmetric with the fread fix.

  • test_programs/unistd/fdopen_flags.c: new test covering all fdopen mode strings and the fcntl(F_GETFL)+O_ACCMODE pattern used by BFD.

- fdopen/fopen: replace FLAG_IS_SET(open_mode, O_RDONLY) with
  O_ACCMODE mask comparison. FLAG_IS_SET with O_RDONLY=0 always
  returns true, causing IOBF_READ to be set on write-only streams.

- fcntl F_GETFL: include O_RDONLY/O_WRONLY/O_RDWR and O_APPEND bits
  in the returned flags. Previously F_GETFL only returned O_NONBLOCK/
  O_ASYNC/O_PATH, breaking the BFD-style 'fdflags & O_ACCMODE' switch.

- fread: move cantread() check before the fast path. The fast path
  bypassed cantread when the buffer was already allocated, allowing
  reads on write-only streams to silently succeed.

- fwrite: add cantwrite() check before the fast path, symmetric with
  the fread fix.

- test_programs/unistd/fdopen_flags.c: new test covering all fdopen
  mode strings and the fcntl(F_GETFL)+O_ACCMODE pattern used by BFD.
@afxgroup
afxgroup merged commit 729ab37 into development Jun 19, 2026
3 checks passed
@afxgroup
afxgroup deleted the fix/fdopen-fread-fwrite-accmode-flags branch June 19, 2026 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant