All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Jeff King <peff@peff.net>
Cc: "Michael Haggerty" <mhagger@alum.mit.edu>,
	"Andreas Schwab" <schwab@linux-m68k.org>,
	"Jáchym Barvínek" <jachymb@gmail.com>,
	git@vger.kernel.org
Subject: Re: [PATCH] tempfile: avoid "ferror | fclose" trick
Date: Fri, 17 Feb 2017 13:17:06 -0800	[thread overview]
Message-ID: <xmqq37fcsejx.fsf@gitster.mtv.corp.google.com> (raw)
In-Reply-To: <20170217205442.wnldfsxbj3dnnqvj@sigill.intra.peff.net> (Jeff King's message of "Fri, 17 Feb 2017 15:54:42 -0500")

Jeff King <peff@peff.net> writes:

> On Fri, Feb 17, 2017 at 11:42:25AM +0100, Michael Haggerty wrote:
>
>> On 02/17/2017 09:07 AM, Jeff King wrote:
>> > [...]
>> > That's similar to what I wrote earlier, but if we don't mind overwriting
>> > errno unconditionally, I think just:
>> > 
>> >   errno = EIO; /* covers ferror(), overwritten by failing fclose() */
>> >   err |= ferror(fp);
>> >   err |= fclose(fp);
>> > 
>> > does the same thing.
>> 
>> True; I'd forgotten the convention that non-failing functions are
>> allowed to change errno. Your solution is obviously simpler and faster.
>
> I guess we are simultaneously assuming that it is OK to munge errno on
> success in our function, but that fclose() will not do so. Which seems a
> bit hypocritical. Maybe the "if" dance is better.

Yes.  When both ferror() and fclose() are successful, we would
prefer to see the original errno unmolested, so the "if" dance,
even though it looks uglier, is better.  The ugliness is limited
to the implementation anyway ;-)

But it does look ugly, especially when nested inside the existing
code like so.

Stepping back a bit, would this be really needed?  Even if the ferror()
does not update errno, the original stdio operation that failed
would have, no?

-- >8 --
Subject: close_tempfile(): set errno when ferror() notices a previous error

In close_tempfile(), we may notice that previous stdio operations
failed when we inspect ferror(tempfile->fp).  As ferror() does not
set errno, and the caller of close_tempfile(), since it encountered
and ignored the original error, is likely to have called other
system library functions to cause errno to be modified, the caller
cannot really tell anything meaningful by looking at errno after we
return an error from here.  

Set errno to an arbitrary value EIO when ferror() sees an error but
fclose() succeeds.  If fclose() fails, we just let the caller see
errno from that failure.

---
 tempfile.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/tempfile.c b/tempfile.c
index ffcc272375..d2c6de83a9 100644
--- a/tempfile.c
+++ b/tempfile.c
@@ -247,8 +247,20 @@ int close_tempfile(struct tempfile *tempfile)
 	tempfile->fd = -1;
 	if (fp) {
 		tempfile->fp = NULL;
-		err = ferror(fp);
-		err |= fclose(fp);
+		if (ferror(fp)) {
+			err = -1;
+			if (!fclose(fp))
+				/*
+				 * There was some error detected by ferror()
+				 * but it is likely that the true errno has
+				 * long gone.  Leave something generic to make
+				 * it clear that the caller cannot rely on errno
+				 * at this point.
+				 */
+				errno = EIO;
+		} else {
+			err = fclose(fp);
+		}
 	} else {
 		err = close(fd);
 	}

  parent reply	other threads:[~2017-02-17 21:17 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-12 16:37 Confusing git messages when disk is full Jáchym Barvínek
2017-02-15 21:32 ` Jeff King
2017-02-15 21:47   ` Junio C Hamano
2017-02-15 21:51     ` Jeff King
2017-02-15 22:28       ` Junio C Hamano
2017-02-15 22:32         ` Jeff King
2017-02-15 22:50           ` Junio C Hamano
2017-02-15 23:18             ` Jeff King
2017-02-16 10:10               ` Andreas Schwab
2017-02-16 16:44                 ` Jeff King
2017-02-16 21:31                   ` [PATCH] tempfile: avoid "ferror | fclose" trick Jeff King
2017-02-17  8:00                     ` Michael Haggerty
2017-02-17  8:07                       ` Jeff King
2017-02-17 10:42                         ` Michael Haggerty
2017-02-17 20:54                           ` Jeff King
2017-02-17 21:07                             ` Jeff King
2017-02-17 21:17                             ` Junio C Hamano [this message]
2017-02-17 21:21                               ` Jeff King
2017-02-17 21:42                                 ` Junio C Hamano
2017-02-17 22:10                                   ` Jeff King
2017-02-17 22:40                                     ` Junio C Hamano
2017-02-17 23:39                                       ` Jeff King
2017-02-17 23:52                                         ` Junio C Hamano
2017-02-17 23:54                                           ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqq37fcsejx.fsf@gitster.mtv.corp.google.com \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=jachymb@gmail.com \
    --cc=mhagger@alum.mit.edu \
    --cc=peff@peff.net \
    --cc=schwab@linux-m68k.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.