musl - musl - an implementation of the standard library for Linux-based systems

index : musl
musl - an implementation of the standard library for Linux-based systems
summary refs log tree commit diff
path: root/src/stdlib/atol.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011年11月10日 20:44:44 -0500
committerRich Felker <dalias@aerifal.cx>2011年11月10日 20:44:44 -0500
commit0c4188f6d76fad021a93eb1012630c717bda80a1 (patch)
treecf750ce90574fa55b066de623c0ce42005a26545 /src/stdlib/atol.c
parent3ed8c9f2df0b5f0bfe1006037c46d4f32ec6ca7b (diff)
downloadmusl-0c4188f6d76fad021a93eb1012630c717bda80a1.tar.gz
fix signed overflows at most-negative values in ato(i|l|ll)
patch by Pascal Cuoq (with minor tweaks to comments)
Diffstat (limited to 'src/stdlib/atol.c')
-rw-r--r--src/stdlib/atol.c 5
1 files changed, 3 insertions, 2 deletions
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;
}
generated by cgit v1.2.1 (git 2.18.0) at 2025年09月20日 15:08:12 +0000

AltStyle によって変換されたページ (->オリジナル) /