From 0c4188f6d76fad021a93eb1012630c717bda80a1 Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Thu, 10 Nov 2011 20:44:44 -0500 Subject: fix signed overflows at most-negative values in ato(i|l|ll) patch by Pascal Cuoq (with minor tweaks to comments) --- src/stdlib/atoi.c | 5 +++-- src/stdlib/atol.c | 5 +++-- src/stdlib/atoll.c | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/stdlib/atoi.c b/src/stdlib/atoi.c index 648b154f..9baca7b8 100644 --- a/src/stdlib/atoi.c +++ b/src/stdlib/atoi.c @@ -9,7 +9,8 @@ int atoi(const char *s) case '-': neg=1; case '+': s++; } + /* Compute n as a negative number to avoid overflow on INT_MIN */ while (isdigit(*s)) - n = 10*n + *s++ - '0'; - return neg ? -n : n; + n = 10*n - (*s++ - '0'); + return neg ? n : -n; } diff --git a/src/stdlib/atol.c b/src/stdlib/atol.c index 9c91bba9..140ea3ea 100644 --- a/src/stdlib/atol.c +++ b/src/stdlib/atol.c @@ -10,7 +10,8 @@ long atol(const char *s) case '-': neg=1; case '+': s++; } + /* Compute n as a negative number to avoid overflow on LONG_MIN */ while (isdigit(*s)) - n = 10*n + *s++ - '0'; - return neg ? -n : n; + n = 10*n - (*s++ - '0'); + return neg ? n : -n; } diff --git a/src/stdlib/atoll.c b/src/stdlib/atoll.c index 0e03e0a1..b6930489 100644 --- a/src/stdlib/atoll.c +++ b/src/stdlib/atoll.c @@ -10,7 +10,8 @@ long long atoll(const char *s) case '-': neg=1; case '+': s++; } + /* Compute n as a negative number to avoid overflow on LLONG_MIN */ while (isdigit(*s)) - n = 10*n + *s++ - '0'; - return neg ? -n : n; + n = 10*n - (*s++ - '0'); + return neg ? n : -n; } -- cgit v1.2.1