diff options
Diffstat (limited to 'userland/libc/stdlib/strtol.c')
-rw-r--r-- | userland/libc/stdlib/strtol.c | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/userland/libc/stdlib/strtol.c b/userland/libc/stdlib/strtol.c index 09a5ad7..30d04ec 100644 --- a/userland/libc/stdlib/strtol.c +++ b/userland/libc/stdlib/strtol.c @@ -4,8 +4,7 @@ #include <limits.h> #include <stdlib.h> -extern int errno; -extern int get_value(char c, long base); +int get_value(char c, long base); // https://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html long strtol(const char *str, char **restrict endptr, int base) { @@ -35,11 +34,15 @@ long strtol(const char *str, char **restrict endptr, int base) { if (2 <= base && 36 >= base) { for (; *str; str++) { + if (ret_value > LONG_MAX / base) { + errno = ERANGE; + return LONG_MAX; + } ret_value *= base; int val = get_value(*str, base); if (ret_value > LONG_MAX - val) { errno = ERANGE; - return 0; + return LONG_MAX; } ret_value += val; } @@ -48,6 +51,6 @@ long strtol(const char *str, char **restrict endptr, int base) { return 0; } if (endptr) - *endptr = (char*)str; + *endptr = (char *)str; return ret_value; } |