From cb20965d6f349aebefa67d896850f3409a043c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 23 Oct 2024 11:41:55 +0200 Subject: [PATCH] fix(lstmap): free content if alloc for new node fails --- ft_lstmap_bonus.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ft_lstmap_bonus.c b/ft_lstmap_bonus.c index 621bf6d..c9c8423 100644 --- a/ft_lstmap_bonus.c +++ b/ft_lstmap_bonus.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/21 17:14:36 by kcolin #+# #+# */ -/* Updated: 2024/10/21 17:28:42 by kcolin ### ########.fr */ +/* Updated: 2024/10/23 11:41:00 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,14 +16,17 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) { t_list *out; t_list *next; + void *new_content; out = NULL; while (lst != NULL) { - next = ft_lstnew((*f)(lst->content)); + new_content = (*f)(lst->content); + next = ft_lstnew(new_content); if (next == NULL) { ft_lstclear(&out, del); + (*del)(new_content); return (NULL); } ft_lstadd_back(&out, next);