[gccsdk] fread bug

Alan Buckley alan_baa at hotmail.com
Tue Sep 6 05:42:56 PDT 2011

It appears there is a bug in the C fread function in unixlib.

I expected the feof call in the following code to return
a non-zero value if the Testfile is less than 4096, instead
I get zero.

#include <stdio.h>

int main()
    char buffer[4096];
    FILE *fp;

    fp = fopen("Testfile", "r");
    if (fp)
       int bytes = fread(buffer, 1, 4096, fp);
       printf("Bytes read %d feof %d\n", bytes, (int)feof(fp));

    return 0;

I was using GCC 4.1.1 (GCCSDK release 2).

Note: In the original code I discovered this in I did check
ferror(fp) and that was not set either.

I'm assuming this is a bug as the documentation I have found
on the internet on fread states feof should be set.


More information about the gcc mailing list