mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Pascal Cuoq <pascal.cuoq@gmail.com>
To: musl@lists.openwall.com
Subject: Undefined behavior in atoi()
Date: Sun, 6 Nov 2011 15:24:00 +0100	[thread overview]
Message-ID: <CAOH62JjfMW366hJzT=UKRPnmwq_mqmYOhFyvkGaRn0n30Sdj=w@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 595 bytes --]

Hello,

the attached patch against musl-0.8.3
removes an undefined behavior when atoi()
is applied to the representation of INT_MIN.

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

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().

Regards,

Pascal

[-- Attachment #1.2: Type: text/html, Size: 868 bytes --]

[-- Attachment #2: patch_atoi --]
[-- Type: application/octet-stream, Size: 1224 bytes --]

--- atol.orig.c	2011-11-06 14:49:24.000000000 +0100
+++ atol.c	2011-11-06 14:52:16.000000000 +0100
@@ -10,7 +10,9 @@
 	case '-': neg=1;
 	case '+': s++;
 	}
+  /* Compute n as a negative number to avoid undefined behavior 
+     when s represents LONG_MIN on 2’s complement archs: */
 	while (isdigit(*s))
-		n = 10*n + *s++ - '0';
-	return neg ? -n : n;
+		n = 10*n - (*s++ - '0');
+	return neg ? n : -n;
 }
--- atoll.c~	2011-09-22 02:24:48.000000000 +0200
+++ atoll.c	2011-11-06 14:54:52.000000000 +0100
@@ -10,7 +10,9 @@
 	case '-': neg=1;
 	case '+': s++;
 	}
+  /* Compute n as a negative number to avoid undefined behavior 
+     when s represents LLONG_MIN on 2’s complement archs: */
 	while (isdigit(*s))
-		n = 10*n + *s++ - '0';
-	return neg ? -n : n;
+		n = 10*n - (*s++ - '0');
+	return neg ? n : -n;
 }
--- atoi.c~	2011-09-22 02:24:48.000000000 +0200
+++ atoi.c	2011-11-06 14:53:36.000000000 +0100
@@ -9,7 +9,9 @@
 	case '-': neg=1;
 	case '+': s++;
 	}
+ /* Compute n as a negative number to avoid undefined behavior 
+     when s represents INT_MIN on 2’s complement archs: */
 	while (isdigit(*s))
-		n = 10*n + *s++ - '0';
-	return neg ? -n : n;
+		n = 10*n - (*s++ - '0');
+	return neg ? n : -n;
 }

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

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-06 14:24 Pascal Cuoq [this message]
2011-11-06 21:21 ` Rich Felker
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='CAOH62JjfMW366hJzT=UKRPnmwq_mqmYOhFyvkGaRn0n30Sdj=w@mail.gmail.com' \
    --to=pascal.cuoq@gmail.com \
    --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).