diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-10-11 14:49:01 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-10-11 14:49:01 +0200 |
| commit | 306a69eb9ae88813cf0be0aa3e001481e12220a1 (patch) | |
| tree | cdbd341a01a0b7ff35b003f028c4e88babc33efe /src/str/ft_strtol.c | |
| parent | 783260a4c01ebfe4b4bcfc56d57a4d99603f904f (diff) | |
| download | libft-306a69eb9ae88813cf0be0aa3e001481e12220a1.tar.gz libft-306a69eb9ae88813cf0be0aa3e001481e12220a1.tar.bz2 libft-306a69eb9ae88813cf0be0aa3e001481e12220a1.zip | |
Fixing ft_strtol endptr not initialized on overflow
Diffstat (limited to 'src/str/ft_strtol.c')
| -rw-r--r-- | src/str/ft_strtol.c | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/src/str/ft_strtol.c b/src/str/ft_strtol.c index 7e4cb30..499e54c 100644 --- a/src/str/ft_strtol.c +++ b/src/str/ft_strtol.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/15 10:26:45 by cacharle #+# #+# */ -/* Updated: 2020/10/11 14:03:49 by cacharle ### ########.fr */ +/* Updated: 2020/10/11 14:32:46 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,19 +39,27 @@ static int st_strtol_handle_base(int base, const char **str) return (10); } -static long st_errno_return(int err, long ret) +static void st_setup_base(char base_str[37], int base) { + ft_strncpy(base_str, STRTOL_STD_BASE, base); + base_str[base] = '\0'; +} + +static long st_return(int err, char **end, long ret) +{ + if (end != NULL) + *end = NULL; errno = err; return (ret); } /* -** If there is no digits doesn't put str in endptr like the original, +** If there is no digits doesn't put str in end like the original, ** instead it puts the address of the char after spaces and sign. ** Too much lines and annoyance, I can't be bothered. */ -long ft_strtol(const char *str, char **endptr, int base) +long ft_strtol(const char *str, char **end, int base) { t_ftbool is_negative; unsigned long long nb; @@ -63,18 +71,19 @@ long ft_strtol(const char *str, char **endptr, int base) if (*str == '-' || *str == '+') str++; if ((base = st_strtol_handle_base(base, &str)) == -1) - return (st_errno_return(EINVAL, 0)); - ft_strncpy(base_str, STRTOL_STD_BASE, base); - base_str[base] = '\0'; + return (st_return(EINVAL, end, 0)); + st_setup_base(base_str, base); nb = 0; while (*str != '\0' && ft_strchr(base_str, *str) != NULL) { nb *= base; if (is_negative ? (nb > -(unsigned long)LONG_MIN) : (nb > LONG_MAX)) - return (st_errno_return(ERANGE, is_negative ? LONG_MIN : LONG_MAX)); + return (st_return(ERANGE, end, is_negative ? LONG_MIN : LONG_MAX)); nb += ft_strchr(base_str, ft_tolower(*str++)) - base_str; + if (is_negative ? (nb > -(unsigned long)LONG_MIN) : (nb > LONG_MAX)) + return (st_return(ERANGE, end, is_negative ? LONG_MIN : LONG_MAX)); } - if (endptr != NULL) - *endptr = (char*)str; + if (end != NULL) + *end = (char*)str; return (is_negative ? -nb : nb); } |
