fixed readline() leak/double_free with **environ

This commit is contained in:
LuckyLaszlo
2021-12-02 01:35:02 +01:00
parent b3f74c4179
commit d710f5ef68
3 changed files with 19 additions and 33 deletions

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 02:59:58 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 // variable globale
int g_switch_heredoc_sigint; int g_switch_heredoc_sigint;
extern char **environ; extern char **environ;
char **g_ori_environ; // WIP free test
// Init // Init
int init(t_all *c); int init(t_all *c);

View File

@@ -6,41 +6,18 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/10 23:53:17 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" #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) int exit_free(t_all *c, int exit_status)
{ {
free(c->prompt_base); free(c->prompt_base);
free(c->prompt); free(c->prompt);
ft_lstclear((t_list **)&c->token_list, free); ft_lstclear((t_list **)&c->token_list, free);
//ft_free_2d_arr(environ); 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(c->path); ft_free_2d_arr(c->path);
free_pipeline(&c->pipeline); free_pipeline(&c->pipeline);
//if (c->termios_changed) //if (c->termios_changed)

View File

@@ -6,25 +6,25 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */ /* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 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" #include "minishell.h"
static char *init_prompt_base(void); static char *init_prompt_base(void);
static int init_readline_hook(void);
int init(t_all *c) int init(t_all *c)
{ {
ft_bzero(c, sizeof (*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); 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) if (!environ)
return (ft_reti_perror(0, "ft_dup_2d_arr(environ) error")); return (ft_reti_perror(0, "ft_dup_2d_arr(environ) error"));
//environ = NULL; // WIP free test c->path = retrieve_path();
//print_matrix(c->envp, "\n --- \n"); // TEST WIP
c->path = retrieve_path(); // No return check. Its intended. PATH is optional
c->prompt_base = init_prompt_base(); c->prompt_base = init_prompt_base();
if (!c->prompt_base) if (!c->prompt_base)
return (ft_reti_perror(0, "init_prompt_base() error")); 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); prompt = ft_strjoinfree_s1(prompt, TERM_RESET U_PROMPT_END);
return (prompt); 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);
}