compiler errors with fread/fwrite and fortify source

Matthias Klose doko at
Tue Mar 24 10:04:14 GMT 2009

Starting with intrepid and fortify source turned on by default, the compiler
unconditionally gives a warning for ignored return values for function calls of
functions which are declared with __attribute__((warn_unused_result)). Building
with -Werror turns the warnings into errors, which cannot be disabled (by
design) [1].

glibc declares both fread and fwrite (and fwrite_unlocked) with
__attribute__((warn_unused_result)). In [2] you find an argument why this may be

> The reason why it is a glibc bug is that it is very over the top of adding the
> attribute here.

And indeed there is no logical difference between printf and fwrite here,
but glibc is marking fwrite and not printf.

In both cases, a valid programming style is to use fflush and ferror at
the end to check for errors, rather than checking on every write, or to
check the return value of fclose.  A program that uses fwrite without
checking the return value or such a subsequent error is buggy - so is one
using printf and failing later to check for errors on stdout.  (GCC is
among such buggy programs; "gcc --help >/dev/full" does not return error
status as it should.)  But checking at the end suffices (albeit losing
information about the value of errno for the original error), you don't
need to check at every call.

Our current practice to fix this in packages is to either turn of -Werror, or
patch the sources to introduce a dummy variable.  Proposing to remove the
attribute for fwrite/fwrite_unlocked for jaunty.



More information about the ubuntu-devel mailing list