From b6bb3f42fca1d8f3358251ca65f1c0613c9c35fe Mon Sep 17 00:00:00 2001 From: Tsunghao Date: Wed, 4 Sep 2024 13:38:18 +0200 Subject: [PATCH] Fixed segfault and potential leak when frames failed to load --- includes/cub3d.h | 3 ++- srcs/game_init.c | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/includes/cub3d.h b/includes/cub3d.h index e8f2d59..b445a48 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */ -/* Updated: 2024/09/04 12:28:56 by tsuchen ### ########.fr */ +/* Updated: 2024/09/04 13:37:03 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,6 +59,7 @@ # define MOUSE_PRESS 4 # define MOUSE_MOVE 6 # define FILE_EXTENSION ".cub" +# define FRAME_SCRIPT "./assets/animations/door_frame.txt" typedef enum e_texture t_texture; typedef struct s_textdata t_textdata; diff --git a/srcs/game_init.c b/srcs/game_init.c index 1ea624b..8c1499b 100644 --- a/srcs/game_init.c +++ b/srcs/game_init.c @@ -6,7 +6,7 @@ /* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/27 17:03:43 by jteissie #+# #+# */ -/* Updated: 2024/09/03 18:58:48 by tsuchen ### ########.fr */ +/* Updated: 2024/09/04 13:36:40 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,8 +31,10 @@ static int init_frame(char *line, t_data *data, t_frame *f) path = ft_strtrim(line, "\n"); if (!path) - return (1); + return (PANIC); f->img = mlx_xpm_file_to_image(data->mlx, path, &f->frm_w, &f->frm_h); + if (!f->img) + return (free(path), PANIC); f->addr = mlx_get_data_addr(f->img, &f->bpp, &f->ll, &f->endian); lst_add_back(&data->frames, f); free(path); @@ -45,9 +47,9 @@ static int load_frames(t_data *data) int frame_fd; char *line; - frame_fd = open("./assets/animations/door_frame.txt", O_RDONLY); + frame_fd = open(FRAME_SCRIPT, O_RDONLY); if (frame_fd == -1) - return (SUCCESS); + return (PANIC); line = get_next_line(frame_fd); if (!line) return (PANIC); @@ -57,7 +59,7 @@ static int load_frames(t_data *data) if (!f) return (close(frame_fd), PANIC); if (init_frame(line, data, f)) - return (free(line), close(frame_fd), PANIC); + return (free(line), close(frame_fd), free(f), PANIC); free(line); line = get_next_line(frame_fd); }