Skip to content

Commit e11ddac

Browse files
committed
fix: Fix memory leaks if ft_lst* function args are NULL
1 parent 7cbda04 commit e11ddac

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

libraries/libft/src/lists/singly_linked/ft_lstadd_back_tail.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/02/11 00:35:15 by ldulling #+# #+# */
9-
/* Updated: 2025/05/28 19:29:42 by ldulling ### ########.fr */
9+
/* Updated: 2025/05/28 21:34:35 by ldulling ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -20,15 +20,22 @@
2020
*
2121
* @param lst The address of the pointer to the first node of the list.
2222
* @param tail The address of the pointer to the last node of the list.
23+
* If tail (not *tail) is NULL, it is ignored.
2324
* @param new The new node to be added to the list.
25+
* If new is NULL, the function does nothing.
2426
*
2527
* @return No return value.
2628
*
2729
*/
2830
void ft_lstadd_back_tail(t_list **lst, t_list **tail, t_list *new)
2931
{
30-
if (*lst == NULL)
31-
*lst = new;
32+
if (lst == NULL || tail == NULL || new == NULL)
33+
{
34+
ft_lstadd_back(lst, new);
35+
return ;
36+
}
37+
if (*lst == NULL || *tail == NULL)
38+
ft_lstadd_back(lst, new);
3239
else
3340
(*tail)->next = new;
3441
*tail = new;

libraries/libft/src/lists/singly_linked/ft_lstnew_back.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/01/06 20:30:19 by ldulling #+# #+# */
9-
/* Updated: 2024/01/17 11:42:57 by ldulling ### ########.fr */
9+
/* Updated: 2025/05/28 20:14:28 by ldulling ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -20,13 +20,15 @@
2020
* @param content The content to be added to the new node.
2121
*
2222
* @return Returns true if the new node was successfully added, false
23-
* if malloc failed.
23+
* if malloc failed or lst (not *lst) is NULL.
2424
*
2525
*/
2626
bool ft_lstnew_back(t_list **lst, void *content)
2727
{
2828
t_list *new_node;
2929

30+
if (lst == NULL)
31+
return (false);
3032
new_node = ft_lstnew(content);
3133
if (new_node == NULL)
3234
return (false);

libraries/libft/src/lists/singly_linked/ft_lstnew_back_tail.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/07 19:11:17 by ldulling #+# #+# */
9-
/* Updated: 2025/05/28 19:26:46 by ldulling ### ########.fr */
9+
/* Updated: 2025/05/28 20:24:43 by ldulling ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -20,16 +20,19 @@
2020
*
2121
* @param lst The address of the list to add the new node to.
2222
* @param tail The address of the pointer to the last node of the list.
23+
* If tail (not *tail) is NULL, it is ignored.
2324
* @param content The content to be added to the new node.
2425
*
2526
* @return Returns true if the new node was successfully added, false
26-
* if malloc failed.
27+
* if malloc failed or lst (not *lst) is NULL.
2728
*
2829
*/
2930
bool ft_lstnew_back_tail(t_list **lst, t_list **tail, void *content)
3031
{
3132
t_list *new_node;
3233

34+
if (lst == NULL || tail == NULL)
35+
return (ft_lstnew_back(lst, content));
3336
new_node = ft_lstnew(content);
3437
if (new_node == NULL)
3538
return (false);

libraries/libft/src/lists/singly_linked/ft_lstnew_front.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/01/25 16:09:14 by ldulling #+# #+# */
9-
/* Updated: 2024/01/25 16:09:17 by ldulling ### ########.fr */
9+
/* Updated: 2025/05/28 20:14:18 by ldulling ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -20,13 +20,15 @@
2020
* @param content The content to be added to the new node.
2121
*
2222
* @return Returns true if the new node was successfully added, false if
23-
* malloc failed.
23+
* malloc failed or lst (not *lst) is NULL.
2424
*
2525
*/
2626
bool ft_lstnew_front(t_list **lst, void *content)
2727
{
2828
t_list *new_node;
2929

30+
if (lst == NULL)
31+
return (false);
3032
new_node = ft_lstnew(content);
3133
if (new_node == NULL)
3234
return (false);

0 commit comments

Comments
 (0)