musl/src/stdlib/atoi.c
Rich Felker 0c4188f6d7 fix signed overflows at most-negative values in ato(i|l|ll)
patch by Pascal Cuoq (with minor tweaks to comments)
2011-11-10 20:44:44 -05:00

17 lines
300 B
C

#include <stdlib.h>
#include <ctype.h>
int atoi(const char *s)
{
int n=0, neg=0;
while (isspace(*s)) s++;
switch (*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;
}