Page MenuHomeFreeBSD

cmp: Check the status of stdout.
ClosedPublic

Authored by des on Oct 8 2024, 10:02 PM.
Tags
None
Referenced Files
Unknown Object (File)
Wed, Dec 25, 11:32 AM
Unknown Object (File)
Mon, Dec 9, 5:56 AM
Unknown Object (File)
Wed, Dec 4, 10:18 PM
Unknown Object (File)
Nov 25 2024, 7:43 PM
Unknown Object (File)
Nov 21 2024, 1:13 AM
Unknown Object (File)
Nov 19 2024, 8:57 AM
Unknown Object (File)
Nov 17 2024, 7:49 AM
Unknown Object (File)
Nov 16 2024, 1:50 PM
Subscribers

Details

Summary

POSIX requires us to print an error message and exit non-zero if
writing to stdout fails. This can only happen if sflag is unset.

MFC after: 3 days
Sponsored by: Klara, Inc.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

des requested review of this revision.Oct 8 2024, 10:02 PM

There are two many separate exit points, so install an atexit handler before calling anything that may print to stdout.

But almost all of them are exiting with a non-zero status. Don't you just need to handle those that exit with status 0?

There are two many separate exit points, so install an atexit handler before calling anything that may print to stdout.

But almost all of them are exiting with a non-zero status. Don't you just need to handle those that exit with status 0?

Strictly speaking, yes- other errors should take precedence over a flush error, even if we could have encountered the flush error first but didn't bother error checking all of the writes to stdout.

DIFF_EXIT is non-zero but not an error. Unless -s was specified, it is preceded by a message to stdout, and POSIX requires us to not only exit non-zero but also print an error message to stderr if writing to stdout fails. I suppose this would be clearer if I rewrote cmp to return non-error results back to main so there was only a single non-error exit point, and then try to fflush(stdout) iff !sflag, rather than use atexit().

Thank you, I think this is nicer.

This revision is now accepted and ready to land.Oct 9 2024, 12:03 PM
This revision was automatically updated to reflect the committed changes.
usr.bin/cmp/tests/cmp_test2.sh
144

If I run this test case in a loop with some load in the background, e.g., from a build, there is an occasional failure because the result is 1 instead of 2.

I wonder if the test is racy because the subshell is able to write to the pipe buffer before the other end exits, triggering EPIPE?