mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@aerifal.cx>
To: musl@lists.openwall.com
Subject: Re: Undefined behavior in atoi()
Date: Sun, 6 Nov 2011 16:21:45 -0500	[thread overview]
Message-ID: <20111106212145.GO132@brightrain.aerifal.cx> (raw)
In-Reply-To: <CAOH62JjfMW366hJzT=UKRPnmwq_mqmYOhFyvkGaRn0n30Sdj=w@mail.gmail.com>

On Sun, Nov 06, 2011 at 03:24:00PM +0100, Pascal Cuoq wrote:
> Hello,
> 
> the attached patch against musl-0.8.3
> removes an undefined behavior when atoi()
> is applied to the representation of INT_MIN.

Thanks. How did you manage to find this bug? Just browsing source?

> The undefined behavior is not observable
> if the compiler implements 2's complement
> for signed arithmetic overflows, but the compiler
> doesn't have to.

musl does assume/require a twos complement representation for signed
values, but of course even with this, signed overflow is UB.

> On the other hand, C99 mandates either two's complement's
> lopsided representation of integers or other,
> symmetrical, representations (6.2.6.2), so I think the patch
> is an overall improvement.
> 
> The patch applies in musl-0.8.3/src/stdlib/
> and contains identical changes for atol() and atoll().

I was thinking about some alternate fixes, but they turned out to be
wrong. The only other one I'm mildly considering is just making atoi,
etc. wrappers for strtol, etc. This should reduce the overall size of
the library at the cost of increasing the size of tiny programs that
just use atoi, but since atoi has UB when the value overflows, it's
second only to gets in unusability for correct programs...

Still leaning towards applying your patch as-is.

Rich


  reply	other threads:[~2011-11-06 21:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-06 14:24 Pascal Cuoq
2011-11-06 21:21 ` Rich Felker [this message]
2011-11-06 22:28   ` Pascal Cuoq
2011-11-08  3:44     ` Rich Felker
2011-11-08  5:43       ` Pascal Cuoq
2011-11-08 14:12         ` Rich Felker

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=20111106212145.GO132@brightrain.aerifal.cx \
    --to=dalias@aerifal.cx \
    --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).