fix logic error in fread
authorRich Felker <dalias@aerifal.cx>
Sun, 17 Jul 2011 01:24:02 +0000 (21:24 -0400)
committerRich Felker <dalias@aerifal.cx>
Sun, 17 Jul 2011 01:24:02 +0000 (21:24 -0400)
fread was calling f->read without checking that the file was in
reading mode. this could:
1. crash, if f->read was a null pointer
2. cause unwanted blocking on a terminal already at eof
3. allow reading on a write-only file

src/stdio/fread.c

index 8105fe9..5c23577 100644 (file)
@@ -21,14 +21,9 @@ size_t fread(void *destv, size_t size, size_t nmemb, FILE *f)
                l -= k;
        }
        
                l -= k;
        }
        
-       if (!l) {
-               FUNLOCK(f);
-               return nmemb;
-       }
-
        /* Read the remainder directly */
        for (; l; l-=k, dest+=k) {
        /* Read the remainder directly */
        for (; l; l-=k, dest+=k) {
-               k = f->read(f, dest, l);
+               k = __toread(f) ? 0 : f->read(f, dest, l);
                if (k+1<=1) {
                        FUNLOCK(f);
                        return (len-l)/size;
                if (k+1<=1) {
                        FUNLOCK(f);
                        return (len-l)/size;