Openwall GNU/*/Linux - a small security-enhanced Linux distro for servers
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Fri, 8 Jun 2012 13:00:56 -0400
From: Rich Felker <dalias@...ifal.cx>
To: musl@...ts.openwall.com
Subject: Re: printf POSIX compliance

On Fri, Jun 08, 2012 at 05:46:10PM +0100, Reuben Thomas wrote:
> On 8 June 2012 17:46, John Spencer <maillist-musl@...fooze.de> wrote:
> >
> > this is bogus, according to Rich:
> > "all files are closed when a process terminates normally/calls exit.
> >  if you want to report write failures, just fflush(stdout) before exit and
> > check the return value"
> 
> Jim Meyering has an analysis of the problem here:
> 
> http://www.gnu.org/ghm/2011/paris/#sec-2-1

He makes it a lot more difficult than it has to be. My preferred way
of handling the situation is never to close stdout at all, only to
flush it with fflush(stdout), then check ferror(stdout). This will
report any new or past write errors that weren't already cleared by
clearerr; the only errors it cannot report are error returns from the
underlying close(1) operation. Some people want to detect such errors,
in which case the following expression will reliably determine if any
error occurred:

ferror(stdout) || fclose(stdout)

With that said, I question the value of checking for errors in
close(). The only historical case where they have any consequence are
with broken NFS setups (NFS is broken beyond usability for countless
reasons, but this is getting off-topic...), where even the lack of an
error from close() cannot ensure consistency.

There's certainly not a need to do any of this with atexit functions
or other ugly hacks. Whatever error check you do, it belongs at the
point of normal program termination (or if you're handling multiple
output files, perhaps the point where you finish handling stdout).

Rich

Powered by blists - more mailing lists

Your e-mail address:

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.