From 99bd88f639921c95c7c160b4d7f8e5066d32d8d0 Mon Sep 17 00:00:00 2001 From: Jean Teissier Date: Fri, 16 Aug 2024 14:43:50 +0200 Subject: [PATCH] Weird get_next_line double free crash instead of returning null pointer when EOF is reached, swapping for my own to test --- Makefile | 4 ++-- includes/cub3d.h | 8 +++++++- includes/parser.h | 6 ++++-- srcs/main.c | 10 ++++++---- srcs/parser/parser.c | 45 +++++++++++++++++++++++++++++++------------- 5 files changed, 51 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index 0c5d9ff..063ad01 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: jteissie +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # 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 # # # # **************************************************************************** # @@ -36,7 +36,7 @@ OBJS = $(SRCS:.c=.o) HEAD = includes/ -CFLAGS = -Wall -Wextra -Werror # -g +CFLAGS = -Wall -Wextra -Werror -g3 CC = cc diff --git a/includes/cub3d.h b/includes/cub3d.h index bcdb96b..e9828c5 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -46,4 +46,10 @@ # define MOUSE_MOVE 6 # define CLOSE_BUTTON 17 +typedef enum e_bool +{ + FALSE, + TRUE, +} t_bool; + #endif diff --git a/includes/parser.h b/includes/parser.h index 18eb836..a2818c3 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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 diff --git a/srcs/main.c b/srcs/main.c index bc3a473..de9c273 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -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); diff --git a/srcs/parser/parser.c b/srcs/parser/parser.c index 4732694..e5a6d1c 100644 --- a/srcs/parser/parser.c +++ b/srcs/parser/parser.c @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 */ /* */ /* ************************************************************************** */ @@ -14,6 +14,19 @@ // 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; @@ -21,12 +34,15 @@ static char **build_map(int map_fd) 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); @@ -34,6 +50,7 @@ static char **build_map(int map_fd) return (NULL); } map[index] = line; + free(line); line = get_next_line(map_fd); index++; } @@ -41,22 +58,22 @@ static char **build_map(int map_fd) 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; @@ -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); } @@ -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); } @@ -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); }