From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/520 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: Undefined behavior in atoi() Date: Sun, 6 Nov 2011 16:21:45 -0500 Message-ID: <20111106212145.GO132@brightrain.aerifal.cx> References: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1320615193 26204 80.91.229.12 (6 Nov 2011 21:33:13 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sun, 6 Nov 2011 21:33:13 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-521-gllmg-musl=m.gmane.org@lists.openwall.com Sun Nov 06 22:33:09 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 1RNAKy-0001Yq-74 for gllmg-musl@lo.gmane.org; Sun, 06 Nov 2011 22:33:08 +0100 Original-Received: (qmail 17691 invoked by uid 550); 6 Nov 2011 21:33:07 -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 17681 invoked from network); 6 Nov 2011 21:33:07 -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:520 Archived-At: 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