diff options
| author | Jakob Kaivo <jkk@ung.org> | 2024-05-27 12:49:46 -0400 |
|---|---|---|
| committer | Jakob Kaivo <jkk@ung.org> | 2024-05-27 12:49:46 -0400 |
| commit | 04e5527eeca240ca81b0b95b58c50ef6a16b8030 (patch) | |
| tree | 3623519b07b9f3c027fa13fac925923dd05a3baa /src/stdio/freopen.c | |
| parent | 9b784a3affb4027c9bc40fba2b272abe7e580ece (diff) | |
handle UB for invalid fflush() operations and mixing input and output without intervening fflush() or repositioning
Diffstat (limited to 'src/stdio/freopen.c')
| -rw-r--r-- | src/stdio/freopen.c | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/src/stdio/freopen.c b/src/stdio/freopen.c index d431b0e3..f2238ff9 100644 --- a/src/stdio/freopen.c +++ b/src/stdio/freopen.c @@ -78,10 +78,12 @@ FILE * freopen(const char * restrict filename, const char * restrict mode, FILE } flockfile(stream); - fflush(stream); + if (stream->write) { + fflush(stream); + } if (filename != NULL) { - fd = open(filename, openmode, 0); + fd = open(filename, openmode, 0755); /* TODO: umask */ if (fd < 0) { /* open() already sets errno */ funlockfile(stream); @@ -103,6 +105,8 @@ FILE * freopen(const char * restrict filename, const char * restrict mode, FILE stream->nvalid_ftell = 0; stream->text = !(strchr(mode, 'b')); + stream->read = ((openmode & O_RDONLY) == O_RDONLY) || ((openmode & O_RDWR) == O_RDWR); + stream->write = ((openmode & O_WRONLY) == O_WRONLY) || ((openmode & O_RDWR) == O_RDWR); /* RETURN_SUCCESS(ARGUMENT(stream)); |
