fix get_prev_alloc, add "invalid pointer" message when an invalid pointer is passed to free or realloc

This commit is contained in:
2024-12-04 18:49:53 +01:00
parent 7dfa7eb049
commit 913b33f74b
3 changed files with 20 additions and 18 deletions

12
main.c
View File

@ -3,14 +3,8 @@
int main(void) int main(void)
{ {
int i; void *ptr;
i = 0;
while(i < 110)
{
malloc(256);
i++;
}
show_alloc_mem();
ptr = malloc(1203);
free(ptr);
} }

View File

@ -6,12 +6,19 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/30 18:46:07 by tomoron #+# #+# */ /* Created: 2024/11/30 18:46:07 by tomoron #+# #+# */
/* Updated: 2024/12/03 18:59:13 by tomoron ### ########.fr */ /* Updated: 2024/12/04 18:23:00 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "includes/malloc.h" #include "includes/malloc.h"
void invalid_pointer(char *fnc)
{
(void)fnc;
write(2, fnc, ft_strlen(fnc));
write(2, "(): invalid pointer\n", 21);
}
t_mem_chunk *get_alloc_chunk(t_alloc *alloc, t_mem_chunk *first, int is_small) t_mem_chunk *get_alloc_chunk(t_alloc *alloc, t_mem_chunk *first, int is_small)
{ {
size_t size; size_t size;
@ -30,7 +37,7 @@ t_mem_chunk *get_alloc_chunk(t_alloc *alloc, t_mem_chunk *first, int is_small)
return (0); return (0);
} }
int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur) int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur, char *fnc)
{ {
t_alloc *prev; t_alloc *prev;
@ -45,7 +52,7 @@ int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur)
*res = cur; *res = cur;
return (1); return (1);
} }
if (cur->next > *alloc && cur <= *alloc && ((t_ul)alloc - \ if ((cur->next > *alloc || cur->next == 0) && cur <= *alloc && ((t_ul)*alloc - \
(t_ul)cur) <= cur->size) (t_ul)cur) <= cur->size)
{ {
*alloc = cur; *alloc = cur;
@ -55,6 +62,7 @@ int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur)
prev = cur; prev = cur;
cur = cur->next; cur = cur->next;
} }
invalid_pointer(fnc);
return (0); return (0);
} }
@ -68,7 +76,7 @@ static int free_prealloc(t_alloc *alloc, t_mem_chunk **main_chunk, \
chunk = get_alloc_chunk(alloc, *main_chunk, is_small); chunk = get_alloc_chunk(alloc, *main_chunk, is_small);
if (!chunk) if (!chunk)
return (0); return (0);
if (!get_prev_alloc(&alloc, &prev, chunk->first)) if (!get_prev_alloc(&alloc, &prev, chunk->first, "free"))
return (1); return (1);
chunk->space_left -= alloc->size + sizeof(t_alloc); chunk->space_left -= alloc->size + sizeof(t_alloc);
if (chunk->first == alloc) if (chunk->first == alloc)
@ -92,7 +100,7 @@ static void free_large(t_alloc *alloc)
{ {
t_alloc *prev; t_alloc *prev;
if (!get_prev_alloc(&alloc, &prev, g_allocs.large)) if (!get_prev_alloc(&alloc, &prev, g_allocs.large, "free"))
return ; return ;
if (alloc == g_allocs.large) if (alloc == g_allocs.large)
g_allocs.large = alloc->next; g_allocs.large = alloc->next;

View File

@ -6,14 +6,14 @@
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/02 14:41:22 by tomoron #+# #+# */ /* Created: 2024/12/02 14:41:22 by tomoron #+# #+# */
/* Updated: 2024/12/03 18:57:50 by tomoron ### ########.fr */ /* Updated: 2024/12/04 17:22:37 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "includes/malloc.h" #include "includes/malloc.h"
t_mem_chunk *get_alloc_chunk(t_alloc *alloc, t_mem_chunk *first, size_t size); t_mem_chunk *get_alloc_chunk(t_alloc *alloc, t_mem_chunk *first, size_t size);
int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur); int get_prev_alloc(t_alloc **alloc, t_alloc **res, t_alloc *cur, char *fnc);
static void *realloc_recreate(t_alloc *alloc, size_t size) static void *realloc_recreate(t_alloc *alloc, size_t size)
{ {
@ -35,7 +35,7 @@ static void *realloc_prealloc(t_alloc *alloc, t_mem_chunk *chunk, \
chunk = get_alloc_chunk(alloc, chunk, is_small); chunk = get_alloc_chunk(alloc, chunk, is_small);
if (!chunk) if (!chunk)
return (0); return (0);
if (!get_prev_alloc(&alloc, &prev, chunk->first)) if (!get_prev_alloc(&alloc, &prev, chunk->first, "realloc"))
return (0); return (0);
if (alloc->size >= size) if (alloc->size >= size)
{ {
@ -56,7 +56,7 @@ static void *realloc_large(t_alloc *alloc, size_t size)
{ {
t_alloc *prev; t_alloc *prev;
if (!get_prev_alloc(&alloc, &prev, g_allocs.large)) if (!get_prev_alloc(&alloc, &prev, g_allocs.large, "realloc"))
return (0); return (0);
return (realloc_recreate(alloc, size)); return (realloc_recreate(alloc, size));
} }