fix leak in list_remove_if

This commit is contained in:
2025-04-12 20:49:55 +02:00
parent 5d98a06bf4
commit 5f5ea11549
2 changed files with 42 additions and 26 deletions

View File

@ -1,39 +1,48 @@
global ft_list_remove_if global ft_list_remove_if
extern free
section .text section .text
;this function remove an element from a list, set the previous element to the next
; the element ot remove is in rax
; the previouse element is in r8
; the pointer to the start of the list is in rdi
remove_element: remove_element:
push rsi
mov rsi, [rax + 8]
call call_free_elem call call_free_elem
push rax
mov rax, [rax + 8]
test r8, r8 test r8, r8
jz element_is_first jz first_elem
mov [r8 + 8], rax mov [r8 + 8], rsi
pop rax jmp remove_element_end
first_elem:
mov [rdi], rsi
remove_element_end:
mov rax, rsi
pop rsi
ret ret
element_is_first: ;wants the element that needs to be freed in rax
mov [rdi], rax
pop rax
ret
call_free_elem: call_free_elem:
push rdi push rdi
push rsi push rsi
push rdx push rdx
push rcx push rcx
push rax
push r8 push r8
push rax
mov rdi, [rax] mov rdi, [rax]
call rcx call rcx
pop rdi
call free wrt ..plt
pop r8 pop r8
pop rax
pop rcx pop rcx
pop rdx pop rdx
pop rsi pop rsi
@ -78,8 +87,9 @@ loop:
jz loop_end jz loop_end
mov rax, [rsp] pop rax
call remove_element call remove_element
jmp loop
loop_end: loop_end:
@ -89,7 +99,5 @@ loop_end:
jmp loop jmp loop
end: end:
ret ret

24
test.c
View File

@ -6,7 +6,7 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/29 19:53:58 by tomoron #+# #+# */ /* Created: 2025/03/29 19:53:58 by tomoron #+# #+# */
/* Updated: 2025/04/12 19:08:05 by tomoron ### ########.fr */ /* Updated: 2025/04/12 19:30:05 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,6 +38,12 @@
fail++;\ fail++;\
} }
void remove_if_free(void *data)
{
printf("%ld\n", (long int) data);
}
int test_strlen(void) int test_strlen(void)
{ {
int fail = 0; int fail = 0;
@ -65,7 +71,6 @@ int test_strcpy(void)
return(fail); return(fail);
} }
int test_strcmp(void) int test_strcmp(void)
{ {
int fail = 0; int fail = 0;
@ -234,6 +239,11 @@ int test_list_sort()
ft_list_sort(0, 0); ft_list_sort(0, 0);
TEST("null pointer (should not segfault)", 1); TEST("null pointer (should not segfault)", 1);
free(tmp->next->next->next);
free(tmp->next->next);
free(tmp->next);
free(tmp);
return(fail); return(fail);
} }
@ -242,11 +252,6 @@ int remove_if_compare(void *data, int *nb)
return((long)data == *nb); return((long)data == *nb);
} }
void remove_if_free(void *data)
{
printf("%ld\n", (long int) data);
}
int test_list_remove_if() int test_list_remove_if()
{ {
int fail = 0; int fail = 0;
@ -268,6 +273,9 @@ int test_list_remove_if()
ft_list_remove_if(&tmp, &nb, remove_if_compare, 0); ft_list_remove_if(&tmp, &nb, remove_if_compare, 0);
TEST("null pointers in functions", 1); TEST("null pointers in functions", 1);
free(tmp->next);
free(tmp);
return(fail); return(fail);
} }
@ -294,5 +302,5 @@ int main(void)
printf("\n\n"); printf("\n\n");
FNC_TEST("ft_list_sort", test_list_sort()); FNC_TEST("ft_list_sort", test_list_sort());
printf("\n\n"); printf("\n\n");
FNC_TEST("ft_list_sort", test_list_remove_if()); FNC_TEST("ft_list_remove_if", test_list_remove_if());
} }