tempfile: avoid "ferror | fclose" trick
commit0838cbc22fc9567ede7a60e800d876e733820060
authorJeff King <peff@peff.net>
Thu, 16 Feb 2017 21:31:40 +0000 (16 16:31 -0500)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 Feb 2017 22:15:55 +0000 (16 14:15 -0800)
treea2a90069273587574ef65ae6287b54431f4b0468
parentc3808ca6982b0ad7ee9b87eca9b50b9a24ec08b0
tempfile: avoid "ferror | fclose" trick

The current code wants to record an error condition from
either ferror() or fclose(), but makes sure that we always
call both functions. So it can't use logical-OR "||", which
would short-circuit when ferror() is true. Instead, it uses
bitwise-OR "|" to evaluate both functions and set one or
more bits in the "err" flag if they reported a failure.

Unlike logical-OR, though, bitwise-OR does not introduce a
sequence point, and the order of evaluation for its operands
is unspecified. So a compiler would be free to generate code
which calls fclose() first, and then ferror() on the
now-freed filehandle.

There's no indication that this has happened in practice,
but let's write it out in a way that follows the standard.

Noticed-by: Andreas Schwab <schwab@linux-m68k.org>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
tempfile.c