From f23592ea0c122a0da2723d4253c47ca227529f79 Mon Sep 17 00:00:00 2001 From: Charles Date: Mon, 14 Oct 2019 12:54:57 +0200 Subject: Fixed ft_split to destroy previous alloc if ENOMEM - Normed libf.h - Removed ugly conditions in ft_strnstr --- ft_split.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'ft_split.c') diff --git a/ft_split.c b/ft_split.c index e3b312c..74703fb 100644 --- a/ft_split.c +++ b/ft_split.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:22:09 by cacharle #+# #+# */ -/* Updated: 2019/10/07 14:30:10 by cacharle ### ########.fr */ +/* Updated: 2019/10/14 12:51:34 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,6 +48,17 @@ static char *ft_strndup(const char *s1, size_t n) return (clone); } +static void *destroy_strs(char **strs) +{ + int i; + + i = 0; + while (strs[i] != NULL) + free(strs[i++]); + free(strs); + return (NULL); +} + char **ft_split(char const *s, char c) { char **strs; @@ -69,9 +80,10 @@ char **ft_split(char const *s, char c) i = 0; while (s[i] && s[i] != c) i++; - strs[j++] = ft_strndup(s, i); + if ((strs[j++] = ft_strndup(s, i)) == NULL) + return (destroy_strs(strs)); s += i; } - strs[j] = 0; + strs[j] = NULL; return (strs); } -- cgit