fix: potential leak with gnl due to non-exhaustion

This commit is contained in:
Khaïs COLIN 2025-07-17 13:38:59 +02:00
parent 085a51ca6d
commit d7efc43f12
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
3 changed files with 23 additions and 4 deletions

View file

@ -23,5 +23,6 @@ int keyrelease_handler(int keycode, t_cub3d_data *data);
void my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color); void my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color);
void draw_2d_wall(unsigned int color, t_mlx_data *data, int x, int y); void draw_2d_wall(unsigned int color, t_mlx_data *data, int x, int y);
void draw_map(t_mapdata *map, t_player *player, t_mlx_data *data); void draw_map(t_mapdata *map, t_player *player, t_mlx_data *data);
void gnl_exhaust(int fd);
#endif #endif

View file

@ -12,6 +12,7 @@
#include "../../includes/maputils.h" #include "../../includes/maputils.h"
#include "../../includes/libft.h" #include "../../includes/libft.h"
#include "../../includes/cub3d.h"
unsigned long set_color(const char *s, t_mapdata *map) unsigned long set_color(const char *s, t_mapdata *map)
{ {
@ -79,7 +80,7 @@ bool add_textures(int fd, t_mapdata *map)
while (line && set_lines < 6) while (line && set_lines < 6)
{ {
if (!set_textures(line, map)) if (!set_textures(line, map))
return (free(line), false); return (free(line), gnl_exhaust(fd), false);
else else
set_lines++; set_lines++;
free(line); free(line);

View file

@ -3,16 +3,17 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* frees.c :+: :+: :+: */ /* frees.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */ /* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/25 17:46:01 by tchampio #+# #+# */ /* Created: 2025/07/17 13:59:27 by kcolin #+# #+# */
/* Updated: 2025/07/15 10:26:48 by tchampio ### ########.fr */ /* Updated: 2025/07/17 14:00:01 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../includes/cub3d_consts.h" #include "../../includes/cub3d_consts.h"
#include "../../includes/structs.h" #include "../../includes/structs.h"
#include "../../mlx/mlx.h" #include "../../mlx/mlx.h"
#include "../../includes/libft.h"
#include <stdlib.h> #include <stdlib.h>
void free_tab(char **tab) void free_tab(char **tab)
@ -56,3 +57,19 @@ int destroy(t_cub3d_data *data)
exit(0); exit(0);
return (0); return (0);
} }
/*
Calls get_next_line in a loop until NULL is returned, freeing all data returned.
Ensures that all memory allocated by get_next_line is properly freed.
*/
void gnl_exhaust(int fd)
{
char *line;
line = get_next_line(fd);
while (line != NULL)
{
free(line);
line = get_next_line(fd);
}
}