mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: fwrite() - possible division by zero
Date: Wed, 14 Feb 2018 23:20:42 -0500	[thread overview]
Message-ID: <20180215042042.GR1627@brightrain.aerifal.cx> (raw)
In-Reply-To: <CACJ51z0p382Vr2aoqh8H8_BLnyz2Bn0vzahYgmX9Z2cDVW7Phg@mail.gmail.com>

On Wed, Feb 14, 2018 at 04:47:46PM -0500, Andrew Bell wrote:
> On Wed, Feb 14, 2018 at 4:15 PM, Szabolcs Nagy <nsz@port70.net> wrote:
> 
> > * Andrew Bell <andrew.bell.ia@gmail.com> [2018-02-14 15:11:34 -0500]:
> > > On Wed, Feb 14, 2018 at 3:07 PM, Markus Wichmann <nullplan@gmx.net>
> > wrote:
> > >
> > > > On Wed, Feb 14, 2018 at 02:48:14PM -0500, Andrew Bell wrote:
> > > > > Why not early return if size == 0 and avoid the call to __fwritex
> > > > > altogether?
> > > > >
> > > >
> > > > Because it's a rare corner case? Here, there's also locking correctness
> > > > to consider: fwrite() has to block until f is unlocked, irrespective of
> > > > parameters. So there's no real benefit to doing an early return.
> > > >
> > >
> > > But it's already being checked to set nmemb to 0.  Couldn't you just
> > return
> > > 0 and avoid the lock as well?
> >
> > the lock must not be avoided.
> >
> > otherwise fwrite would make progress on a FILE locked by
> > another thread which is non-conforming.
> 
> 
> That's not how I read this: http://port70.net/~nsz/c/c11/n1570.html#7.21.2p8
> 
> "All functions that read, write, position, or query the position of a
> stream lock the stream before accessing it.
> They release the lock associated with the stream when the access is
> complete."
> 
> When size == 0, the FILE doesn't need to be accessed so no lock should be
> necessary.
> Perhaps language of this document has been superseded?
> 
> But it doesn't much matter.  It just seemed to make the code more clear to
> me and would have avoided the initial question.

It's a POSIX requirement:

	"All functions that reference (FILE *) objects, except those
	with names ending in _unlocked, shall behave as if they use
	flockfile() and funlockfile() internally to obtain ownership
	of these (FILE *) objects."

http://pubs.opengroup.org/onlinepubs/9699919799/functions/flockfile.html

Rich


  reply	other threads:[~2018-02-15  4:20 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-14 18:24 Geraldo Netto
2018-02-14 19:39 ` Markus Wichmann
2018-02-14 19:48   ` Andrew Bell
2018-02-14 20:07     ` Markus Wichmann
2018-02-14 20:11       ` Andrew Bell
2018-02-14 21:15         ` Szabolcs Nagy
2018-02-14 21:47           ` Andrew Bell
2018-02-15  4:20             ` Rich Felker [this message]
2018-02-16  2:58               ` Geraldo Netto

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=20180215042042.GR1627@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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).