From d710f5ef682423d633a9875495342b75e5d950d9 Mon Sep 17 00:00:00 2001 From: LuckyLaszlo Date: Thu, 2 Dec 2021 01:35:02 +0100 Subject: [PATCH] fixed readline() leak/double_free with **environ --- headers/minishell_prototypes.h | 3 +-- srcs/free.c | 27 ++------------------------- srcs/init.c | 22 ++++++++++++++++------ 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index ad4c5b3..2c5267d 100644 --- a/headers/minishell_prototypes.h +++ b/headers/minishell_prototypes.h @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 02:59:58 by lperrey #+# #+# */ -/* Updated: 2021/12/01 17:17:49 by lperrey ### ########.fr */ +/* Updated: 2021/12/02 00:37:39 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,6 @@ // variable globale int g_switch_heredoc_sigint; extern char **environ; -char **g_ori_environ; // WIP free test // Init int init(t_all *c); diff --git a/srcs/free.c b/srcs/free.c index d21ffc5..285350b 100644 --- a/srcs/free.c +++ b/srcs/free.c @@ -6,41 +6,18 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/10 23:53:17 by lperrey #+# #+# */ -/* Updated: 2021/11/29 12:43:29 by lperrey ### ########.fr */ +/* Updated: 2021/12/02 00:42:33 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -// WIP : un peu zob. Previent les invalid free de readline, mais peut leak si "LINES" ou "COLUMNS" sont redefini avec export -void ft_free_2d_arr_ENVIRON(void *ptr) -{ - unsigned int i; - char **arr; - - if (!ptr) - return ; - arr = (char **)ptr; - i = 0; - while (arr[i]) - { - if (ft_strncmp(arr[i], "LINES=", 5 + 1) != 0 && ft_strncmp(arr[i], "COLUMNS=", 7 + 1) != 0) - free(arr[i]); - i++; - } - free(arr); -} - int exit_free(t_all *c, int exit_status) { free(c->prompt_base); free(c->prompt); ft_lstclear((t_list **)&c->token_list, free); - //ft_free_2d_arr(environ); - ft_free_2d_arr_ENVIRON(environ); - free(g_ori_environ); - //environ = g_ori_environ; // WIP free test - environ = NULL; // WIP free test + ft_free_2d_arr(environ); ft_free_2d_arr(c->path); free_pipeline(&c->pipeline); //if (c->termios_changed) diff --git a/srcs/init.c b/srcs/init.c index 0c20481..11f9ef0 100644 --- a/srcs/init.c +++ b/srcs/init.c @@ -6,25 +6,25 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */ -/* Updated: 2021/11/27 23:50:06 by lperrey ### ########.fr */ +/* Updated: 2021/12/02 01:32:18 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" static char *init_prompt_base(void); +static int init_readline_hook(void); int init(t_all *c) { ft_bzero(c, sizeof (*c)); + rl_startup_hook = init_readline_hook; + readline(NULL); + rl_startup_hook = NULL; environ = ft_dup_2d_arr(environ, (t_dup_f)ft_strdup); - g_ori_environ = environ; // Wip free test - //environ = ft_resize_2d_arr(environ, 0); // WIP free test if (!environ) return (ft_reti_perror(0, "ft_dup_2d_arr(environ) error")); - //environ = NULL; // WIP free test - //print_matrix(c->envp, "\n --- \n"); // TEST WIP - c->path = retrieve_path(); // No return check. Its intended. PATH is optional + c->path = retrieve_path(); c->prompt_base = init_prompt_base(); if (!c->prompt_base) return (ft_reti_perror(0, "init_prompt_base() error")); @@ -78,3 +78,13 @@ char *init_prompt(char *prompt_base) // WIP, error return TODO prompt = ft_strjoinfree_s1(prompt, TERM_RESET U_PROMPT_END); return (prompt); } + +/* +** set rl_startup_hook with this, for init COLUMNS and LINES variables +** and prevent leak/double_free with **environ +*/ +static int init_readline_hook(void) +{ + rl_done = 1; + return (0); +}