aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2019-11-03 00:33:42 +0100
committerCharles <sircharlesaze@gmail.com>2019-11-03 00:33:42 +0100
commit3e54b8257ed66c8f10fa904655f554990931ab88 (patch)
treee3ecdb1b7aff8522f744bb7956453510c645346e
parent0a966722ec0236521d99706632a7fe56b7245379 (diff)
downloadget_next_line-3e54b8257ed66c8f10fa904655f554990931ab88.tar.gz
get_next_line-3e54b8257ed66c8f10fa904655f554990931ab88.tar.bz2
get_next_line-3e54b8257ed66c8f10fa904655f554990931ab88.zip
Fixed rest and buf memory leaks
-rw-r--r--get_next_line.c29
-rw-r--r--get_next_line.h4
-rw-r--r--get_next_line_utils.c4
3 files changed, 22 insertions, 15 deletions
diff --git a/get_next_line.c b/get_next_line.c
index 3434e5a..62878b8 100644
--- a/get_next_line.c
+++ b/get_next_line.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/19 09:08:36 by cacharle #+# #+# */
-/* Updated: 2019/11/03 00:18:57 by cacharle ### ########.fr */
+/* Updated: 2019/11/03 00:33:16 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -43,8 +43,7 @@ int get_next_line(int fd, char **line)
if (fd < 0 || fd > OPEN_MAX || line == NULL || BUFFER_SIZE <= 0)
return (STATUS_ERROR);
- *line = malloc(1);
- (*line)[0] = 0;
+ *line = NULL;
if (rest[fd] == NULL || rest[fd][0] == 0)
return (read_line(fd, line, &rest[fd]));
if (HAS_NEWLINE(rest[fd], split_at))
@@ -81,13 +80,13 @@ int read_line(int fd, char **line, char **rest)
*rest = ft_strdup(buf + split_at + 1);
buf[split_at] = '\0';
if ((*line = ft_strappend(*line, buf)) == NULL)
- return (free_return(&buf, STATUS_ERROR));
- return (free_return(&buf, STATUS_LINE));
+ return (free_return(&buf, rest, STATUS_ERROR));
+ return (free_return(&buf, NULL, STATUS_LINE));
}
if ((*line = ft_strappend(*line, buf)) == NULL)
- return (free_return(&buf, STATUS_ERROR));
+ return (free_return(&buf, rest, STATUS_ERROR));
}
- return (free_return(&buf, ret));
+ return (free_return(&buf, rest, ret));
}
int find_newline(char *str)
@@ -101,11 +100,17 @@ int find_newline(char *str)
return (-1);
}
-int free_return(char **ptr, int ret)
+int free_return(char **ptr, char **ptr2, int ret)
{
- if (ptr == NULL)
- return (ret);
- free(*ptr);
- *ptr = NULL;
+ if (ptr != NULL)
+ {
+ free(*ptr);
+ *ptr = NULL;
+ }
+ if (ptr2 != NULL)
+ {
+ free(*ptr2);
+ *ptr2 = NULL;
+ }
return (ret);
}
diff --git a/get_next_line.h b/get_next_line.h
index 0924d0c..6a6ccca 100644
--- a/get_next_line.h
+++ b/get_next_line.h
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/09 13:52:59 by cacharle #+# #+# */
-/* Updated: 2019/11/03 00:10:24 by cacharle ### ########.fr */
+/* Updated: 2019/11/03 00:29:53 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -37,7 +37,7 @@ typedef int t_bool;
int get_next_line(int fd, char **line);
int read_line(int fd, char **line, char **rest);
int find_newline(char *str);
-int free_return(char **ptr, int ret);
+int free_return(char **ptr, char **rest, int ret);
/*
** get_next_line_utils.c - helper functions
diff --git a/get_next_line_utils.c b/get_next_line_utils.c
index 92cb208..096fb84 100644
--- a/get_next_line_utils.c
+++ b/get_next_line_utils.c
@@ -6,7 +6,7 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/08 08:52:59 by cacharle #+# #+# */
-/* Updated: 2019/11/02 23:01:40 by cacharle ### ########.fr */
+/* Updated: 2019/11/03 00:26:34 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,6 +18,8 @@ char *ft_strappend(char *dest, char *src)
void *copy;
int dest_len;
+ if (dest == NULL)
+ return (ft_strdup(src));
dest_len = ft_strlen(dest);
if ((copy = (char*)malloc(sizeof(char) * (dest_len + 1))) == NULL)
return (NULL);