aboutsummaryrefslogtreecommitdiff
path: root/get_next_line.c
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 /get_next_line.c
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
Diffstat (limited to 'get_next_line.c')
-rw-r--r--get_next_line.c29
1 files changed, 17 insertions, 12 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);
}