From 976d68677934ece87fc37cd0761512f5d43e0fe6 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 1 Mar 2020 16:00:52 +0100 Subject: [PATCH] Fix fputc(3) potentially miswriting the next byte after flushing. --- libc/stdio/fputc_unlocked.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/libc/stdio/fputc_unlocked.c b/libc/stdio/fputc_unlocked.c index 9868cd26..8ac137c6 100644 --- a/libc/stdio/fputc_unlocked.c +++ b/libc/stdio/fputc_unlocked.c @@ -45,15 +45,10 @@ int fputc_unlocked(int c, FILE* fp) fp->flags |= _FILE_LAST_WRITE; fp->flags &= ~_FILE_STATUS_EOF; - if ( fp->amount_output_buffered == BUFSIZ ) - { - if ( fflush_unlocked(fp) == EOF ) - return EOF; - } - fp->buffer[fp->amount_output_buffered++] = c; - if ( fp->buffer_mode == _IOLBF && c == '\n' ) + if ( fp->amount_output_buffered == BUFSIZ || + (fp->buffer_mode == _IOLBF && c == '\n') ) { if ( fflush_unlocked(fp) == EOF ) return EOF;