From df677252212b7c0cc2b44747ddf56b893db956e3 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Tue, 8 Jan 2013 15:08:37 +0100 Subject: [PATCH] Fix fgets(3) handling EOF incorrectly. --- libc/fgets.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/libc/fgets.cpp b/libc/fgets.cpp index cb785e36..e94babfb 100644 --- a/libc/fgets.cpp +++ b/libc/fgets.cpp @@ -27,20 +27,19 @@ extern "C" char* fgets(char* dest, int size, FILE* fp) { - if ( size <= 0 ) { errno = EINVAL; return NULL; } + if ( size <= 0 ) + return errno = EINVAL, (char*) NULL; int i; for ( i = 0; i < size-1; i++ ) { int c = getc(fp); if ( c == EOF ) - { - if ( ferror(fp) ) { return NULL; } - else { i++; break; } /* EOF */ - } + break; dest[i] = c; if ( c == '\n' ) { i++; break; } } - + if ( !i && (ferror(fp) || feof(fp)) ) + return NULL; dest[i] = '\0'; return dest; }