Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20111108034404.GP132@brightrain.aerifal.cx>
Date: Mon, 7 Nov 2011 22:44:04 -0500
From: Rich Felker <dalias@...ifal.cx>
To: musl@...ts.openwall.com
Subject: Re: Undefined behavior in atoi()

On Sun, Nov 06, 2011 at 11:28:41PM +0100, Pascal Cuoq wrote:
> On Sun, Nov 6, 2011 at 10:21 PM, Rich Felker <dalias@...ifal.cx> wrote:
> 
> >
> > Thanks. How did you manage to find this bug? Just browsing source?
> >
> 
> I was looking at implementations for strtod() (long story for another time)

It should be noted that the current implementation is not correct.
There's lots of loss-of-precision.

> and I noticed that this library was the most pleasant C code I had
> seen in months (seriously), so I lingered a bit. So, the straightforward
> answer is "yes, code review".

Glad to hear you found the code pleasant, at least that part. Some
things (like floating point in vfprintf.c) are a bit ugly.. :)

> The slightly longer answer is that I have done the formal verification of
> "string to number" C functions in the past, found that they were correct,
> noticed the opposite trick, and somehow the relationship
> between the two clicked. So I was looking for a classic pitfall here.

Interesting, I'd never come across this one.

> Since my previous e-mail, I have been analyzing a few more cases for fun,
> and musl's original code also has this issue for the 40 or so values
> below LONG_MAX.
> 
> The problem is then:
> 
> src/stdlib/atol.orig.c:14:[kernel] warning: Signed overflow.
>                   assert (long)((long)10*n)+(long)*tmp_0 ≤
> 9223372036854775807LL;
> 
> That is, you should in any case subtract '0' from the digit before adding
> it in
> at line 14. In the case of atoll() on IA32, it will even be more efficient.

Thanks for catching that too. I'd certainly be glad to hear the
results of any more code review/testing you're up to doing. I've been
a little bit out of touch with working on musl lately due to being
very busy, but I'm reading the list/irc and making occasional
improvements. Development should speed up a lot in a month or so.

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.