fix(lstmap): free content if alloc for new node fails

This commit is contained in:
Khaïs COLIN 2024-10-23 11:41:55 +02:00
parent 33fa2b4a34
commit cb20965d6f

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 17:14:36 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 *out;
t_list *next; t_list *next;
void *new_content;
out = NULL; out = NULL;
while (lst != NULL) while (lst != NULL)
{ {
next = ft_lstnew((*f)(lst->content)); new_content = (*f)(lst->content);
next = ft_lstnew(new_content);
if (next == NULL) if (next == NULL)
{ {
ft_lstclear(&out, del); ft_lstclear(&out, del);
(*del)(new_content);
return (NULL); return (NULL);
} }
ft_lstadd_back(&out, next); ft_lstadd_back(&out, next);