fixed potentiel segfault in malloc.c where I memset ret without checking that ret is not NULL, and used true header size to memsetthe malloc disturb patern, in case malloc allocate a larger chunk than expected

This commit is contained in:
thrieg 2025-12-13 06:39:20 +01:00
parent db2b5f27bb
commit 0384df02e9
2 changed files with 10 additions and 7 deletions

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* ft_malloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg < thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/11/17 15:02:55 by thrieg #+# #+# */
/* Updated: 2025/12/08 16:02:22 by thrieg ### ########.fr */
/* Updated: 2025/12/13 06:31:39 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,7 +33,10 @@ void *malloc(size_t size)
ret = add_tiny(size);
}
pthread_mutex_unlock(&g_mut);
if (g_state.patern)
if (ret && g_state.patern)
{
size = (((t_header *)ret) - 1)->size;
ft_memset(ret, g_state.patern, size); // doesn't need to lock because we only modify the user pointer and the user doesn't have accces to it yet
}
return (ret);
}