mailing list of musl libc
 help / color / mirror / code / Atom feed
* Undefined behavior in atoi()
@ 2011-11-06 14:24 Pascal Cuoq
  2011-11-06 21:21 ` Rich Felker
  0 siblings, 1 reply; 6+ messages in thread
From: Pascal Cuoq @ 2011-11-06 14:24 UTC (permalink / raw)
  To: musl


[-- 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;
 }

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-11-08 14:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-06 14:24 Undefined behavior in atoi() Pascal Cuoq
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

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