fix signed overflows at most-negative values in ato(i|l|ll)
[musl] / src / stdlib / atol.c
index 9c91bba..140ea3e 100644 (file)
@@ -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;
 }