fix: O_ACCMODE flag handling in fdopen/fopen/fread/fwrite/fcntl - #437
Merged
Conversation
- 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.