[gccsdk] fread bug

Martin Wuerthner martin at mw-software.com
Tue Sep 6 07:00:18 PDT 2011

In message <SNT136-ds21F65678988D363DDD96ECF01C0 at phx.gbl>
          "Alan Buckley" <alan_baa at hotmail.com> wrote:

> 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.

> ...
>        int bytes = fread(buffer, 1, 4096, fp);
>        printf("Bytes read %d feof %d\n", bytes, (int)feof(fp));
> ...

> 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.

Yes, this looks like a UnixLib bug. If fread() returns less than the 
requested number of objects, then either feof or ferror must be set. 
The above code works as expected when using Norcroft or when using GCC 
and compiling for the SCL. It goes wrong both with static and shared 
UnixLib with GCC 4.1.1 rel2.

Martin Wuerthner          MW Software          martin at mw-software.com

More information about the gcc mailing list