Fix every malloc error

This commit is contained in:
David Gailleton
2025-11-10 11:27:35 +01:00
parent 37fc319f64
commit cb2edb6a2b
4 changed files with 15 additions and 17 deletions

View File

@@ -70,14 +70,11 @@ $(NAME): $(OBJ)
$(AR) $(ARFLAGS) $(NAME) $(OBJ) $(AR) $(ARFLAGS) $(NAME) $(OBJ)
ranlib $(NAME) ranlib $(NAME)
$(OBJ_DIR)/%.o: %.c | $(OBJ_DIR) $(OBJ_DIR)/%.o: %.c
$(CC) -o $@ -c $< $(CFLAGS) -I$(HEADER) $(CC) -o $@ -c $< $(CFLAGS) -I$(HEADER)
$(OBJ_DIR):
mkdir $@
clean: clean:
rm -f $(OBJ) rm -f $(OBJ) $(BONUS_OBJ)
fclean: clean fclean: clean
rm -f $(NAME) rm -f $(NAME)

View File

@@ -6,7 +6,7 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/09 15:54:38 by dgaillet #+# #+# */ /* Created: 2025/11/09 15:54:38 by dgaillet #+# #+# */
/* Updated: 2025/11/09 16:42:23 by dgaillet ### ########lyon.fr */ /* Updated: 2025/11/10 11:22:19 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -15,20 +15,23 @@
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{ {
t_list *new_lst; t_list *new_lst;
t_list *temp; t_list *temp_lst;
void *temp_content;
if (!lst) if (!lst)
return (NULL); return (NULL);
new_lst = NULL; new_lst = NULL;
while (lst) while (lst)
{ {
temp = ft_lstnew(f(lst->content)); temp_content = f(lst->content);
if (!temp) temp_lst = ft_lstnew(temp_content);
if (!temp_lst)
{ {
del(temp_content);
ft_lstclear(&new_lst, del); ft_lstclear(&new_lst, del);
return (NULL); return (NULL);
} }
ft_lstadd_back(&new_lst, temp); ft_lstadd_back(&new_lst, temp_lst);
lst = lst->next; lst = lst->next;
} }
return (new_lst); return (new_lst);

View File

@@ -6,13 +6,13 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/08 17:50:35 by dgaillet #+# #+# */ /* Created: 2025/11/08 17:50:35 by dgaillet #+# #+# */
/* Updated: 2025/11/08 17:57:02 by dgaillet ### ########lyon.fr */ /* Updated: 2025/11/10 10:58:21 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "libft.h"
void write_nbr(unsigned int n, int fd) static void write_nbr(unsigned int n, int fd)
{ {
if (n) if (n)
{ {

View File

@@ -6,7 +6,7 @@
/* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */ /* By: dgaillet <dgaillet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/06 17:02:58 by dgaillet #+# #+# */ /* Created: 2025/11/06 17:02:58 by dgaillet #+# #+# */
/* Updated: 2025/11/09 20:29:25 by dgaillet ### ########lyon.fr */ /* Updated: 2025/11/10 11:09:15 by dgaillet ### ########lyon.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -33,7 +33,7 @@ static int next_match(char *str, unsigned char c)
int i; int i;
i = 0; i = 0;
while (str[i] != c) while (str[i] && str[i] != c)
i++; i++;
return (i); return (i);
} }
@@ -66,8 +66,6 @@ char **ft_split(char const *s, char c)
{ {
if ((s[j] != c && j == 0) || (s[j] != c && s[j - 1] == c)) if ((s[j] != c && j == 0) || (s[j] != c && s[j - 1] == c))
{ {
if (s[j] == c)
j++;
strs[i] = ft_substr(&s[j], 0, next_match((char *) &s[j], c)); strs[i] = ft_substr(&s[j], 0, next_match((char *) &s[j], c));
if (!strs[i]) if (!strs[i])
return (clear_strs(strs), NULL); return (clear_strs(strs), NULL);
@@ -85,7 +83,7 @@ int main(void)
char **strs; char **strs;
int i; int i;
strs = ft_split("--1-2--3---4----5-----42", ' '); strs = ft_split("---------1-2--3---4----5-----42----", ' ');
i = 0; i = 0;
while (strs[i]) while (strs[i])
{ {