From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/522 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: Undefined behavior in atoi() Date: Mon, 7 Nov 2011 22:44:04 -0500 Message-ID: <20111108034404.GP132@brightrain.aerifal.cx> References: <20111106212145.GO132@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: dough.gmane.org 1320724541 4752 80.91.229.12 (8 Nov 2011 03:55:41 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 8 Nov 2011 03:55:41 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-523-gllmg-musl=m.gmane.org@lists.openwall.com Tue Nov 08 04:55:37 2011 Return-path: Envelope-to: gllmg-musl@lo.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by lo.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1RNcmf-00079A-8l for gllmg-musl@lo.gmane.org; Tue, 08 Nov 2011 04:55:37 +0100 Original-Received: (qmail 24411 invoked by uid 550); 8 Nov 2011 03:55:36 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 24403 invoked from network); 8 Nov 2011 03:55:36 -0000 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Xref: news.gmane.org gmane.linux.lib.musl.general:522 Archived-At: On Sun, Nov 06, 2011 at 11:28:41PM +0100, Pascal Cuoq wrote: > On Sun, Nov 6, 2011 at 10:21 PM, Rich Felker 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