fix bug on invalid fd
This commit is contained in:
parent
b5a957f5a4
commit
25d6ee0ae9
3 changed files with 30 additions and 18 deletions
|
|
@ -1,9 +1,12 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
set -xuo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
for FAIL_AFTER in `seq 1 1000`
|
for FAIL_AFTER in `seq 1 1000`
|
||||||
do
|
do
|
||||||
cc -ggdb -Wall -Wextra -Werror -D FAIL_AFTER=$FAIL_AFTER *.c
|
cc -ggdb -Wall -Wextra -Werror -D FAIL_AFTER=$FAIL_AFTER -D BUFFER_SIZE=$2 *.c
|
||||||
valgrind -q --leak-check=full ./a.out get_next_line.c
|
echo
|
||||||
|
echo "num failed malloc: $FAIL_AFTER"
|
||||||
|
echo
|
||||||
|
valgrind -q --leak-check=full --error-exitcode=1 ./a.out $1
|
||||||
done
|
done
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* 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/11/01 13:26:41 by kcolin ### ########.fr */
|
/* Updated: 2024/11/01 17:02:27 by kcolin ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -61,8 +61,10 @@ char *shorten_buffer(char *buffer)
|
||||||
/*
|
/*
|
||||||
** returns 1 on error, 0 on success
|
** returns 1 on error, 0 on success
|
||||||
*/
|
*/
|
||||||
int setup_buffers(char **buffer, char **read_buffer)
|
int setup(char **buffer, char **read_buffer, int fd)
|
||||||
{
|
{
|
||||||
|
if (fd < 0)
|
||||||
|
return (1);
|
||||||
if (*buffer == NULL)
|
if (*buffer == NULL)
|
||||||
{
|
{
|
||||||
*buffer = malloc(1 * sizeof(char));
|
*buffer = malloc(1 * sizeof(char));
|
||||||
|
|
@ -116,7 +118,7 @@ char *get_next_line(int fd)
|
||||||
char *read_buffer;
|
char *read_buffer;
|
||||||
int num_bytes_read;
|
int num_bytes_read;
|
||||||
|
|
||||||
if (setup_buffers(&buffer, &read_buffer) != 0)
|
if (setup(&buffer, &read_buffer, fd) != 0)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
num_bytes_read = 1;
|
num_bytes_read = 1;
|
||||||
while (num_bytes_read != 0)
|
while (num_bytes_read != 0)
|
||||||
|
|
|
||||||
31
test.c
31
test.c
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/24 11:51:57 by kcolin #+# #+# */
|
/* Created: 2024/10/24 11:51:57 by kcolin #+# #+# */
|
||||||
/* Updated: 2024/10/24 11:59:09 by kcolin ### ########.fr */
|
/* Updated: 2024/11/01 16:48:40 by kcolin ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -17,23 +17,30 @@
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
int i;
|
int line_idx;
|
||||||
|
int file_idx;
|
||||||
char *line;
|
char *line;
|
||||||
|
|
||||||
if (argc == 2)
|
if (argc > 1)
|
||||||
{
|
{
|
||||||
fd = open(argv[1], O_RDONLY);
|
file_idx = 1;
|
||||||
line = "";
|
while (file_idx < argc)
|
||||||
i = 0;
|
|
||||||
while (line != NULL)
|
|
||||||
{
|
{
|
||||||
line = get_next_line(fd);
|
fd = open(argv[file_idx], O_RDONLY);
|
||||||
if (line != NULL)
|
line = "";
|
||||||
|
line_idx = 0;
|
||||||
|
while (line != NULL)
|
||||||
{
|
{
|
||||||
i++;
|
line = get_next_line(fd);
|
||||||
printf("%d\t%s", i, line);
|
if (line != NULL)
|
||||||
free(line);
|
{
|
||||||
|
line_idx++;
|
||||||
|
printf("%d\t%s", line_idx, line);
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
file_idx++;
|
||||||
|
close(fd);
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue