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
extern free
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:
push rsi
mov rsi, [rax + 8]
call call_free_elem
push rax
mov rax, [rax + 8]
test r8, r8
jz element_is_first
jz first_elem
mov [r8 + 8], rax
pop rax
ret
mov [r8 + 8], rsi
jmp remove_element_end
element_is_first:
mov [rdi], rax
pop rax
first_elem:
mov [rdi], rsi
remove_element_end:
mov rax, rsi
pop rsi
ret
;wants the element that needs to be freed in rax
call_free_elem:
push rdi
push rsi
push rdx
push rcx
push rax
push r8
push rax
mov rdi, [rax]
call rcx
pop rdi
call free wrt ..plt
pop r8
pop rax
pop rcx
pop rdx
pop rsi
@ -78,8 +87,9 @@ loop:
jz loop_end
mov rax, [rsp]
pop rax
call remove_element
jmp loop
loop_end:
@ -89,7 +99,5 @@ loop_end:
jmp loop
end:
ret

24
test.c
View File

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