fix a leak, get a double free
This commit is contained in:
parent
179f4b3edf
commit
9725166235
2 changed files with 31 additions and 1 deletions
9
check_failure.sh
Executable file
9
check_failure.sh
Executable 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
|
||||
|
|
@ -6,13 +6,28 @@
|
|||
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <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)
|
||||
{
|
||||
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;
|
||||
out = malloc(len);
|
||||
if (out == NULL)
|
||||
{
|
||||
free((void *)s1);
|
||||
return (NULL);
|
||||
}
|
||||
ft_strlcpy(out, s1, len);
|
||||
ft_strlcat(out, s2, len);
|
||||
free((void *)s1);
|
||||
|
|
@ -149,7 +167,10 @@ char *get_next_line(int fd)
|
|||
}
|
||||
read_buffer = malloc((BUFFER_SIZE + 1) * sizeof(char));
|
||||
if (read_buffer == NULL)
|
||||
{
|
||||
free(buffer);
|
||||
return (NULL);
|
||||
}
|
||||
num_bytes_read = 1;
|
||||
while (num_bytes_read != 0)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue