Skip to content

Commit

Permalink
Weird get_next_line double free crash instead of returning null point…
Browse files Browse the repository at this point in the history
…er when EOF is reached, swapping for my own to test
  • Loading branch information
Jean Teissier authored and Jean Teissier committed Aug 16, 2024
1 parent 762234b commit 99bd88f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: jteissie <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/08/15 12:02:08 by tsuchen #+# #+# #
# Updated: 2024/08/15 14:40:12 by jteissie ### ########.fr #
# Updated: 2024/08/16 14:39:13 by jteissie ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -36,7 +36,7 @@ OBJS = $(SRCS:.c=.o)

HEAD = includes/

CFLAGS = -Wall -Wextra -Werror # -g
CFLAGS = -Wall -Wextra -Werror -g3

CC = cc

Expand Down
8 changes: 7 additions & 1 deletion includes/cub3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */
/* Updated: 2024/08/16 13:43:10 by jteissie ### ########.fr */
/* Updated: 2024/08/16 14:23:50 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -46,4 +46,10 @@
# define MOUSE_MOVE 6
# define CLOSE_BUTTON 17

typedef enum e_bool
{
FALSE,
TRUE,
} t_bool;

#endif
6 changes: 4 additions & 2 deletions includes/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 15:00:57 by jteissie #+# #+# */
/* Updated: 2024/08/16 14:12:23 by jteissie ### ########.fr */
/* Updated: 2024/08/16 14:22:57 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,6 +19,8 @@ typedef enum e_parse_status
MAP_OK,
MAP_ERR,
PANIC_ERR,
} t_parse_status
} t_parse_status;

int parse_map(int map_fd);

#endif
10 changes: 6 additions & 4 deletions srcs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 12:03:33 by tsuchen #+# #+# */
/* Updated: 2024/08/15 16:57:49 by jteissie ### ########.fr */
/* Updated: 2024/08/16 14:35:22 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,9 +15,11 @@
int main(int ac, char *av[])
{
int map_fd;
(void)ac;
(void)av;
//verify arguments

//check for file

if (ac < 2 || ac > 2)
return (EXIT_FAILURE);
map_fd = open(av[1], O_RDONLY);
if (map_fd < 0)
return (EXIT_FAILURE);
Expand Down
45 changes: 32 additions & 13 deletions srcs/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,74 @@
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/15 12:12:00 by tsuchen #+# #+# */
/* Updated: 2024/08/16 14:15:36 by jteissie ### ########.fr */
/* Updated: 2024/08/16 14:42:13 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

#include "cub3d.h"

// 1 are walls, 0 are walkable spaces, whitespaces are walkable spaces. Map is invalid if no walkable spaces or not enclosed by walls

static void panic_free(char **array)
{
u_int32_t i;

i = 0;
while (array[i])
{
free(array[i]);
i++;
}
free(array);
}

static char **build_map(int map_fd)
{
char **map;
char *line;
uint32_t index;

index = 0;
map = ft_calloc(1, sizeof(char **));
if (!map)
return (NULL);
line = get_next_line(map_fd);
if (!line)
return (NULL);
while (line)
{
map[index] == ft_calloc(1, sizeof(char *));
map[index] = ft_calloc(1, sizeof(char *));
if (!map[index])
{
free(line);
panic_free(map);
return (NULL);
}
map[index] = line;
free(line);
line = get_next_line(map_fd);
index++;
}
map[index] = NULL;
return (map);
}

static t_parse_status find_start(uint32_t *coordinates, char **map)
static t_parse_status find_start(uint32_t coordinates[], char **map)
{
uint32_t x;
uint32_t y;

x = 0;
y = 0;
coordinates[0] = -1;
coordinates[1] = -1;
coordinates[0] = 0;
coordinates[1] = 0;
while (map[x][y])
{
while (map[x][y])
{
if (map[x][y] == 'N' || map[x][y] == 'S' || map[x][y] == 'W' || map[x][y] == 'E') //enums please
{
if (coordinates[0] != -1 || coordinates[1] != 1)
if (coordinates[0] != 0 || coordinates[1] != 0)
return (MAP_ERR);
coordinates[0] = x;
coordinates[1] = y;
Expand All @@ -66,7 +83,7 @@ static t_parse_status find_start(uint32_t *coordinates, char **map)
x++;
y = 0;
}
if (coordinates[0] == -1)
if (coordinates[0] == 0 && (map[0][0] == '0' || map[0][0] == '1'))
return (MAP_ERR);
return (MAP_OK);
}
Expand Down Expand Up @@ -149,12 +166,10 @@ t_parse_status verify_map(char **map)

if (check_invalid_chars(map) == MAP_ERR)
return (MAP_ERR);
if (find_start(&start, map) == MAP_ERR)
return (MAP_ERR);
fill_whitespaces(map);
if (find_start(start, map) == MAP_ERR)
return (MAP_ERR);
if (check_walls(map, start[0], start[1]) == MAP_ERR)
return (PANIC);
if (error)
return (MAP_ERR);
return (MAP_OK);
}
Expand All @@ -165,7 +180,11 @@ int parse_map(int map_fd)

map = build_map(map_fd);
if (!map)
return (ft_free(map), PANIC);
return (PANIC);
if (verify_map(map) == MAP_ERR)
return (ft_free(map), PANIC);
{
ft_putstr_fd("Error, implement custom messages here (parse_map)\n", STDERR_FILENO);
return (ft_free_all(map), PANIC);
}
return (SUCCESS);
}

0 comments on commit 99bd88f

Please sign in to comment.