PR kern/164674

David Schultz das at FreeBSD.ORG
Wed Mar 21 21:45:33 UTC 2012

On Mon, Mar 12, 2012, Matthew Story wrote:
> On Sun, Mar 4, 2012 at 2:03 PM, John Baldwin <jhb at> wrote:
> > My standard-parsing fu is weak.  Can some other folks on this list look at
> > this PR, figure out what the correct semantics for *fprintf() in this case,
> > and evaluate the patch?  Thanks.
> libc vfprintf currently behaves differently on un|buffered FILEs with
> ferror set:
> 1. Unbuffered FILEs
>       -> does not transmit any bytes, and returns -1
> 2. Buffered FILEs (fully buffered, or line buffered)
>       -> transmits all bytes, and returns -1
> I believe that this should be reconciled such that vfprintf (and all
> derivatives) returns sucess|failure based on transmission, not taking
> ferror state into account, but an alternate solution would be to reconcile
> the behavior of buffered FILEs with the current behavior of unbuffered
> FILEs (e.g. detect ferror state, transmit no bytes and return -1).

I agree with this assessment.  When the error indicator is set on
a stream, it means that an error has occurred at some point---not
that all subsequent operations on the stream should fail.

There ought to be a less ugly fix than the one proposed.  Probably
the PRINT macro and the various other evil macros in vfprintf()
should set ret to EOF, and the following lines in vfprintf.c should
be removed:

	if (__sferror(fp))
		ret = EOF;

If vfprintf() is fixed so that printing to a buffered stream
always returns success after a successful write (regardless of the
prior state of the stream's error indicator), that should fix the
problem for unbuffered streams automatically.  Unbuffered streams
go through __sbprintf(), which throws away the output if
vfprintf() returns -1.

More information about the freebsd-standards mailing list