Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20111106212145.GO132@brightrain.aerifal.cx>
Date: Sun, 6 Nov 2011 16:21:45 -0500
From: Rich Felker <dalias@...ifal.cx>
To: musl@...ts.openwall.com
Subject: Re: Undefined behavior in atoi()

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

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.