From 69adb1cf2ffd3be9e8d96d1ff8175b753c88516b Mon Sep 17 00:00:00 2001 From: lperrey Date: Wed, 22 Dec 2021 19:34:21 +0100 Subject: [PATCH] retrieve_path() leak fixed --- headers/minishell_prototypes.h | 4 ++-- srcs/builtins/export.c | 4 ++-- srcs/builtins/unset.c | 4 ++-- srcs/init/init.c | 4 ++-- srcs/misc/retrieve_path.c | 18 +++++++++++++----- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/headers/minishell_prototypes.h b/headers/minishell_prototypes.h index d252b47..9ef7d83 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/22 13:50:04 by lperrey ### ########.fr */ +/* Updated: 2021/12/22 19:28:07 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ extern char **environ; // Init int init(t_all *c, char *argv[]); char *update_prompt(char *prompt_base); -char **retrieve_path(void); +int retrieve_path(char **path_stock[]); void set_signals_behaviour(void); // Shell modes diff --git a/srcs/builtins/export.c b/srcs/builtins/export.c index 0709ae6..cde6e75 100644 --- a/srcs/builtins/export.c +++ b/srcs/builtins/export.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/12/03 13:36:54 by lperrey #+# #+# */ -/* Updated: 2021/12/21 22:41:14 by lperrey ### ########.fr */ +/* Updated: 2021/12/22 19:23:41 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ int builtin_export(int argc, char *argv[], t_all *c) else if (ret == ERR_ID) exit_value = EXIT_FAILURE; if (ft_strncmp(argv[i], "PATH=", 5) == 0) - c->path = retrieve_path(); + retrieve_path(&c->path); i++; } return (exit_value); diff --git a/srcs/builtins/unset.c b/srcs/builtins/unset.c index 813d208..ec98d89 100644 --- a/srcs/builtins/unset.c +++ b/srcs/builtins/unset.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/12/05 17:05:05 by lperrey #+# #+# */ -/* Updated: 2021/12/18 13:41:15 by lperrey ### ########.fr */ +/* Updated: 2021/12/22 19:23:50 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ int builtin_unset(int argc, char *argv[], t_all *c) if (unset_env_var(argv[i]) == EXIT_FAILURE) exit_value = EXIT_FAILURE; if (ft_strncmp(argv[i], "PATH", 4 + 1) == 0) - c->path = retrieve_path(); + retrieve_path(&c->path); i++; } return (exit_value); diff --git a/srcs/init/init.c b/srcs/init/init.c index 99dfc53..3ebc734 100644 --- a/srcs/init/init.c +++ b/srcs/init/init.c @@ -6,7 +6,7 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */ -/* Updated: 2021/12/20 22:26:05 by lperrey ### ########.fr */ +/* Updated: 2021/12/22 19:24:04 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ int init(t_all *c, char *argv[]) environ = ft_dup_2d_arr(environ, (t_dup_f)ft_strdup); if (!environ) return (ft_reti_perror(0, "ft_dup_2d_arr(environ)")); - c->path = retrieve_path(); + retrieve_path(&c->path); if (!init_shlvl()) return (ft_reti_perror(0, "init_shlvl()")); open_script_file(c, argv); diff --git a/srcs/misc/retrieve_path.c b/srcs/misc/retrieve_path.c index 27ee2d2..724213e 100644 --- a/srcs/misc/retrieve_path.c +++ b/srcs/misc/retrieve_path.c @@ -6,22 +6,30 @@ /* By: lperrey +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */ -/* Updated: 2021/12/08 06:27:23 by lperrey ### ########.fr */ +/* Updated: 2021/12/22 19:28:49 by lperrey ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -char **retrieve_path(void) +#define WARNING_PATH "Warning, $PATH not set\n" + +int retrieve_path(char **path_stock[]) { char *path; char **path_split; path = getenv("PATH"); if (!path) - return (ft_retp_print(NULL, "minishell: Warning, $PATH not set\n", 2)); + { + ft_free_2d_arr(*path_stock); + *path_stock = NULL; + return (shell_error(WARNING_PATH, "", "", 0)); + } path_split = ft_split(path, ':'); if (!path_split) - return (ft_retp_perror(NULL, "retrieve_path()")); - return (path_split); + return (ft_reti_perror(-1, "retrieve_path()")); + ft_free_2d_arr(*path_stock); + *path_stock = path_split; + return (1); }