fix a leak, get a double free

This commit is contained in:
Khaïs COLIN 2024-11-01 12:26:07 +01:00
parent 179f4b3edf
commit 9725166235
2 changed files with 31 additions and 1 deletions

9
check_failure.sh Executable file
View file

@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -xuo pipefail
for FAIL_AFTER in `seq 1 1000`
do
cc -ggdb -Wall -Wextra -Werror -D FAIL_AFTER=$FAIL_AFTER *.c
valgrind -q --leak-check=full ./a.out get_next_line.c
done

View file

@ -6,13 +6,28 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 20:32:46 by kcolin #+# #+# */ /* Created: 2024/10/23 20:32:46 by kcolin #+# #+# */
/* Updated: 2024/10/31 17:30:56 by kcolin ### ########.fr */ /* Updated: 2024/11/01 12:14:49 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "get_next_line.h" #include "get_next_line.h"
#include <stdlib.h> #include <stdlib.h>
/*
static int num_allocs = 0;
static void *xmalloc(size_t size)
{
if (FAIL_AFTER > 0 && num_allocs++ >= FAIL_AFTER)
{
return 0;
}
return malloc(size);
}
#define malloc(x) xmalloc(x)
*/
char *ft_strchr(const char *s, int c) char *ft_strchr(const char *s, int c)
{ {
size_t i; size_t i;
@ -92,7 +107,10 @@ char *ft_strjoin_free_s1(char const *s1, char const *s2)
len = ft_strlen(s1) + ft_strlen(s2) + 1; len = ft_strlen(s1) + ft_strlen(s2) + 1;
out = malloc(len); out = malloc(len);
if (out == NULL) if (out == NULL)
{
free((void *)s1);
return (NULL); return (NULL);
}
ft_strlcpy(out, s1, len); ft_strlcpy(out, s1, len);
ft_strlcat(out, s2, len); ft_strlcat(out, s2, len);
free((void *)s1); free((void *)s1);
@ -149,7 +167,10 @@ char *get_next_line(int fd)
} }
read_buffer = malloc((BUFFER_SIZE + 1) * sizeof(char)); read_buffer = malloc((BUFFER_SIZE + 1) * sizeof(char));
if (read_buffer == NULL) if (read_buffer == NULL)
{
free(buffer);
return (NULL); return (NULL);
}
num_bytes_read = 1; num_bytes_read = 1;
while (num_bytes_read != 0) while (num_bytes_read != 0)
{ {