git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Phillip Wood <phillip.wood123@gmail.com>
Cc: Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	git@vger.kernel.org, Taylor Blau <me@ttaylorr.com>
Subject: Re: fprintf_ln() is slow
Date: Fri, 28 Jun 2019 06:24:27 -0400	[thread overview]
Message-ID: <20190628102427.GB23052@sigill.intra.peff.net> (raw)
In-Reply-To: <374b237e-a29f-5983-0932-63f1c2ebcbbe@gmail.com>

On Fri, Jun 28, 2019 at 11:03:27AM +0100, Phillip Wood wrote:

> > I considered that, too, but I think it is safe. stdio has its own
> > locking, so every individual call is atomic. The potentially problematic
> > case would be where we switch back from line buffering to no-buffering,
> > and somebody else has written some content into our stack-based buffer
> > (that is about to go out of scope!). But I'd assume that as part of the
> > switch to no-buffering that any stdio implementation would flush out the
> > buffer that it's detaching from (while under lock). Nothing else makes
> > sense.
> 
> The C standard section 7.19.5.6 says that
>   The setvbuf function may be used only after the stream pointed to by
>   a stream has been associated with an open file and before any other
>   operation (other than an unsuccessful call to setvbuf) is performed
>   on the stream.
> 
> The is a note about the buffer that says
>   The buffer has to have a lifetime at least as great as the open
>   stream, so the stream should be closed before a buffer that has
>   automatic storage duration is deallocated upon block exit.
> 
> So changing the buffer in the way that has been proposed is undefined
> behavior on two counts I think.

Oof, thanks for the reference. That is much less safe than I had
imagined. We used to do this kind of setvbuf() munging in vreportf.
Interestingly, it was in released versions for about 2 years, but I
don't recall anybody complaining (we eventually reverted it to have more
flexibility in sanitizing the results before writing them out).

Anyway, I think we're all agreed that's the wrong approach here.

-Peff

      reply	other threads:[~2019-06-28 10:24 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-27  5:25 fprintf_ln() is slow Jeff King
2019-06-27  5:57 ` Jeff King
2019-06-27  9:27   ` Duy Nguyen
2019-06-27 12:18     ` Ævar Arnfjörð Bjarmason
2019-06-27 12:32       ` Duy Nguyen
2019-06-27 18:04         ` Junio C Hamano
2019-06-27 21:26         ` Jeff King
2019-06-27 21:21     ` Jeff King
2019-06-27 21:55       ` Junio C Hamano
2019-06-27 12:00   ` Johannes Schindelin
2019-06-27 21:10     ` Jeff King
2019-06-28 10:03       ` Phillip Wood
2019-06-28 10:24         ` Jeff King [this message]

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=20190628102427.GB23052@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=me@ttaylorr.com \
    --cc=phillip.wood123@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).