From f56fd060021c191397b4bbb600d5e9f4f1203fee Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 8 Sep 2023 12:11:54 +0200 Subject: [PATCH] Repaired the function, so it can return "0" and doesn't write outside allocated memory. --- ft_itoa_base.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ft_itoa_base.c b/ft_itoa_base.c index 7e0b901..45d19ef 100644 --- a/ft_itoa_base.c +++ b/ft_itoa_base.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/05 09:46:11 by ljiriste #+# #+# */ -/* Updated: 2023/09/05 11:46:44 by ljiriste ### ########.fr */ +/* Updated: 2023/09/08 12:09:56 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -73,10 +73,12 @@ char *ft_itoa_base(intmax_t n, const char *base) return (res); if (n < 0) res[0] = '-'; - res[size--] = '\0'; + res[--size] = '\0'; + if (n == 0) + res[0] = base[0]; while (n != 0) { - res[size--] = base[abs_max(n % base_len)]; + res[--size] = base[abs_max(n % base_len)]; n /= base_len; } return (res); -- 2.30.2