From d0cca44913356f8ce15e15216b0e26c2e74b4d06 Mon Sep 17 00:00:00 2001 From: Anton Kling <anton@kling.gg> Date: Wed, 17 Apr 2024 16:55:16 +0200 Subject: LibC: Add more functions that support "long long" integers --- userland/libc/stdlib/strtol.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'userland/libc/stdlib/strtol.c') 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; } -- cgit v1.2.3