mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: Removing stupid, spurious UB in stdio (bikeshed time)
Date: Wed, 27 Apr 2016 14:17:45 -0400	[thread overview]
Message-ID: <20160427181745.GT21636@brightrain.aerifal.cx> (raw)
In-Reply-To: <alpine.LNX.2.20.1604271036150.12369@monopod.intra.ispras.ru>

On Wed, Apr 27, 2016 at 10:56:25AM +0300, Alexander Monakov wrote:
> On Tue, 26 Apr 2016, Rich Felker wrote:
> > There's a lot of nonsense-UB in stdio due to buffer comparisons along
> > the lines of "f->rpos < f->rend". The intent of these comparisons is
> > to simultaneously check that the buffer is initialized for the proper
> > mode (read or write) and that there's data left in it (for reading) or
> > space left (to write) or buffered data to be written out (for write),
> > etc.
> > 
> > Unfortunately, when the buffer is uninitialized for the mode being
> > checked, the comparison becomes NULL<NULL, and while this should
> > obviously be false (since < implies !=), NULL<NULL is actually UB.
> > [snip]
> > So what to do?
> 
> Well, since NULL-NULL and NULL<NULL are well-defined in C++, ... ;)

Ha ha.

> Sorry that I don't offer a more substantial comment; let me just chime in
> on the point that a writeup documenting stdio design, like you say,

OK.

> > I think a good place to start might be coming up with and documenting a
> > clear model for how stdio's buffer internals are supposed to work, what
> > operations are allowed, what invariants hold, etc. based on the above
> > analysis of current UB issues and what the code is doing.
> 
> would be nice to have; you recently noted that setvbuf has restrictions,
> and if there are other non-obvious stuff (especially if musl-specific),
> having it written down should be useful.

Are you talking about the C-standard-imposed restriction that you can
only use setvbuf as the first operation on a new FILE? Or something
else I said that I'm not remembering? I was thinking more about musl's
internally-imposed contracts on internal code (users of the buffer
pointers). Of course external contracts for the stdio API have a role
in determining what the internal interfaces need to be capable of.

Rich


  reply	other threads:[~2016-04-27 18:17 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-26 22:18 Rich Felker
2016-04-27  7:56 ` Alexander Monakov
2016-04-27 18:17   ` Rich Felker [this message]
2016-04-27 18:39     ` Alexander Monakov

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=20160427181745.GT21636@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).