Skip to content

Commit

Permalink
Fixed segfault and potential leak when frames failed to load
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsunghao-C committed Sep 4, 2024
1 parent 4fb7866 commit b6bb3f4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
3 changes: 2 additions & 1 deletion includes/cub3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: tsuchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions srcs/game_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: tsuchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down

0 comments on commit b6bb3f4

Please sign in to comment.