add some more null checks

though apparently not enough?
This commit is contained in:
Khaïs COLIN 2024-10-31 18:02:13 +01:00
parent 9339be5e45
commit 179f4b3edf

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/23 20:32:46 by kcolin #+# #+# */
/* Updated: 2024/10/31 14:11:11 by kcolin ### ########.fr */
/* Updated: 2024/10/31 17:30:56 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -107,9 +107,14 @@ char *shorten_buffer(char *buffer)
size_t outlen;
addr_of_newline = ft_strchr(buffer, '\n');
if (addr_of_newline == 0)
if (addr_of_newline == NULL)
{
outbuf = malloc(1 * sizeof(char));
if (outbuf == NULL)
{
free(buffer);
return (NULL);
}
outbuf[0] = '\0';
}
else
@ -117,6 +122,11 @@ char *shorten_buffer(char *buffer)
index_after_newline = addr_of_newline + 1 - buffer;
outlen = ft_strlen(buffer + index_after_newline) + 1;
outbuf = malloc(outlen * sizeof(char));
if (outbuf == NULL)
{
free(buffer);
return (NULL);
}
ft_strlcpy(outbuf, buffer + index_after_newline, outlen);
}
free(buffer);
@ -130,13 +140,17 @@ char *get_next_line(int fd)
int num_bytes_read;
char *outbuf;
if (buffer == NULL)
{
buffer = malloc(1 * sizeof(char));
if (buffer == NULL)
return (NULL);
buffer[0] = '\0';
}
read_buffer = malloc((BUFFER_SIZE + 1) * sizeof(char));
num_bytes_read = 1;
if (buffer == NULL)
buffer = malloc(sizeof(char));
if (buffer == NULL)
if (read_buffer == NULL)
return (NULL);
buffer[0] = '\0';
num_bytes_read = 1;
while (num_bytes_read != 0)
{
// DONE: if newline found,
@ -169,14 +183,20 @@ char *get_next_line(int fd)
}
read_buffer[num_bytes_read] = '\0';
// DONE: join with big buffer
// FIXME: correctly free buffer
buffer = ft_strjoin_free_s1(buffer, read_buffer);
if (buffer == NULL)
{
free(read_buffer);
return (NULL);
}
}
free(read_buffer);
outbuf = buffer;
if (ft_strlen(buffer) == 0)
{
free(buffer);
buffer = NULL;
outbuf = NULL;
}
return (buffer);
buffer = NULL;
return (outbuf);
}