From 43e6c82cad5d790c75c918a4eca85cdec894d1c8 Mon Sep 17 00:00:00 2001 From: Charles Date: Fri, 18 Oct 2019 15:31:00 +0200 Subject: Fixed ft_substr, added a few protection to list functions --- ft_substr.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'ft_substr.c') diff --git a/ft_substr.c b/ft_substr.c index 1c23e85..88f7203 100644 --- a/ft_substr.c +++ b/ft_substr.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/17 08:28:49 by cacharle #+# #+# */ -/* Updated: 2019/10/17 08:28:55 by cacharle ### ########.fr */ +/* Updated: 2019/10/18 11:55:44 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,15 +18,18 @@ char *ft_substr(char const *s, unsigned int start, size_t len) unsigned int i; char *sub; - if (start > ft_strlen(s)) + if (s == NULL) return (NULL); if ((sub = (char*)malloc(sizeof(char) * (len + 1))) == NULL) return (NULL); i = 0; - while (i < len) + if (!(start > ft_strlen(s))) { - sub[i] = s[start + i]; - i++; + while (i < len && s[start + i]) + { + sub[i] = s[start + i]; + i++; + } } sub[i] = '\0'; return (sub); -- cgit