From 4e9f631918cf6501370fa17be4b8039d67fc6543 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Gu=C3=A9len?= Date: Tue, 6 May 2025 13:36:55 +0200 Subject: [PATCH] fix(group_cmd): prevent fd leak --- .gitignore | 2 + src/executing/group_cmd/group_cmd_execute.c | 6 +-- src/executing/simple_cmd/builtins.c | 17 +++++--- src/executing/simple_cmd/builtins.h | 4 +- .../simple_cmd/handle_redirections.c | 7 +-- .../simple_cmd/handle_redirections.h | 4 +- src/executing/simple_cmd/simple_cmd_execute.c | 43 ++++++++----------- src/executing/simple_cmd/subprocess.c | 7 ++- src/executing/simple_cmd/subprocess.h | 4 +- 9 files changed, 46 insertions(+), 48 deletions(-) diff --git a/.gitignore b/.gitignore index f75ec7f..e767c5e 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,5 @@ fuzz fuzz_hand_tester *CORPUS crash-* +test.sh +std.supp diff --git a/src/executing/group_cmd/group_cmd_execute.c b/src/executing/group_cmd/group_cmd_execute.c index 7c537fe..659f899 100644 --- a/src/executing/group_cmd/group_cmd_execute.c +++ b/src/executing/group_cmd/group_cmd_execute.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/04 19:50:42 by kcolin #+# #+# */ -/* Updated: 2025/04/30 14:06:24 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 14:26:49 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,14 +21,12 @@ t_subprocess group_cmd_execute(t_group_cmd *cmd, t_minishell *app) { int pid; - t_std_fds fds; pid = fork(); if (pid == 0) { - handle_redirections(cmd->redirects, app, &fds); + handle_redirections(cmd->redirects, app); cmd_execute(cmd->cmd, app); - restore_std_fds(&fds); return (SUBPROCESS); } if (pid < 0) diff --git a/src/executing/simple_cmd/builtins.c b/src/executing/simple_cmd/builtins.c index 761d76d..f11b19a 100644 --- a/src/executing/simple_cmd/builtins.c +++ b/src/executing/simple_cmd/builtins.c @@ -5,12 +5,13 @@ /* +:+ +:+ +:+ */ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/01 16:37:21 by kcolin #+# #+# */ -/* Updated: 2025/04/29 15:54:21 by kcolin ### ########.fr */ +/* Created: 2025/05/06 14:48:26 by kcolin #+# #+# */ +/* Updated: 2025/05/06 14:49:50 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #include "builtins.h" +#include "handle_redirections.h" #include "libft.h" #include "simple_cmd_execute.h" #include "std_fds.h" @@ -40,8 +41,9 @@ t_builtin_type get_builtin(t_simple_cmd *cmd) } t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app, - t_std_fds *fds, t_builtin_type type) + t_builtin_type type) { + t_std_fds fds; t_subprocess retvalue; static t_subprocess (*builtins[])(t_simple_cmd *, t_minishell *) = { [BUILTIN_INVALID] = builtin_invalid, @@ -54,8 +56,13 @@ t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app, [BUILTIN_UNSET] = builtin_unset, }; + std_fds_save(&fds); + if (handle_redirections(cmd->redirections, app) == NULL) + { + restore_std_fds(&fds); + return (PARENTPROCESS); + } retvalue = builtins[type](cmd, app); - if (type != BUILTIN_INVALID) - restore_std_fds(fds); + restore_std_fds(&fds); return (retvalue); } diff --git a/src/executing/simple_cmd/builtins.h b/src/executing/simple_cmd/builtins.h index 164778e..8839f49 100644 --- a/src/executing/simple_cmd/builtins.h +++ b/src/executing/simple_cmd/builtins.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/31 14:16:13 by kcolin #+# #+# */ -/* Updated: 2025/04/29 15:46:28 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 14:49:59 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,6 @@ t_subprocess builtin_unset(t_simple_cmd *cmd, t_minishell *app); t_builtin_type get_builtin(t_simple_cmd *cmd); t_subprocess execute_builtin(t_simple_cmd *cmd, t_minishell *app, - t_std_fds *fds, t_builtin_type type); + t_builtin_type type); #endif // BUILTINS_H diff --git a/src/executing/simple_cmd/handle_redirections.c b/src/executing/simple_cmd/handle_redirections.c index 4e966b7..2a3f6eb 100644 --- a/src/executing/simple_cmd/handle_redirections.c +++ b/src/executing/simple_cmd/handle_redirections.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:14:40 by kcolin #+# #+# */ -/* Updated: 2025/04/29 16:55:24 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 14:23:45 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,15 +41,12 @@ static t_redirect *do_redirection(t_redirect *redirection) return (redirection); } -t_minishell *handle_redirections(t_redirect *redirections, t_minishell *app, - t_std_fds *fds) +t_minishell *handle_redirections(t_redirect *redirections, t_minishell *app) { - std_fds_save(fds); while (redirections != NULL) { if (do_redirection(redirections) == NULL) { - restore_std_fds(fds); app->last_return_value = 1; return (NULL); } diff --git a/src/executing/simple_cmd/handle_redirections.h b/src/executing/simple_cmd/handle_redirections.h index a15238f..f401c43 100644 --- a/src/executing/simple_cmd/handle_redirections.h +++ b/src/executing/simple_cmd/handle_redirections.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 11:14:29 by kcolin #+# #+# */ -/* Updated: 2025/04/28 12:45:17 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 14:23:49 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,6 @@ # include "../../minishell.h" t_minishell *handle_redirections(t_redirect *redirections, - t_minishell *app, t_std_fds *fds); + t_minishell *app); #endif // HANDLE_REDIRECTIONS_H diff --git a/src/executing/simple_cmd/simple_cmd_execute.c b/src/executing/simple_cmd/simple_cmd_execute.c index e5c10c9..7f53c3f 100644 --- a/src/executing/simple_cmd/simple_cmd_execute.c +++ b/src/executing/simple_cmd/simple_cmd_execute.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* simple_cmd_execute.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jguelen +#+ +:+ +#+ */ +/* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/03/27 16:21:56 by kcolin #+# #+# */ -/* Updated: 2025/05/02 12:49:22 by jguelen ### ########.fr */ +/* Created: 2025/05/06 14:25:45 by kcolin #+# #+# */ +/* Updated: 2025/05/06 14:59:02 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -76,34 +76,31 @@ static t_subprocess fork_fail(t_minishell *app) return (PARENTPROCESS); } -static t_subprocess exec_external_cmd(t_simple_cmd *cmd, t_minishell *app, - t_std_fds *fds) +static t_subprocess exec_external_cmd(t_simple_cmd *cmd, t_minishell *app) { char *exe; int pid; - if (cmd->words == NULL) - return (restore_std_fds(fds), PARENTPROCESS); - exe = get_cmdpath(cmd->words->word->word, app); - if (exe != NULL) + pid = fork(); + if (pid == 0) { - pid = fork(); - if (pid == 0) - return (execute_subprocess(exe, cmd, app, fds)); - free(exe); - if (pid < 0) - return (fork_fail(app)); - do_waitpid(app, pid); + if (handle_redirections(cmd->redirections, app) == NULL) + return (SUBPROCESS); + if (cmd->words == NULL) + return (SUBPROCESS); + exe = get_cmdpath(cmd->words->word->word, app); + if (exe == NULL) + return (command_not_found(cmd, app), SUBPROCESS); + return (execute_subprocess(exe, cmd, app)); } - restore_std_fds(fds); - if (exe == NULL) - command_not_found(cmd, app); + if (pid < 0) + return (fork_fail(app)); + do_waitpid(app, pid); return (PARENTPROCESS); } t_subprocess simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app) { - t_std_fds fds; t_builtin_type type; if (cmd == NULL) @@ -116,10 +113,8 @@ t_subprocess simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app) return (PARENTPROCESS); } simple_cmd_execute_debug(cmd, app); - if (handle_redirections(cmd->redirections, app, &fds) == NULL) - return (PARENTPROCESS); type = get_builtin(cmd); if (type != BUILTIN_INVALID) - return (execute_builtin(cmd, app, &fds, type)); - return (exec_external_cmd(cmd, app, &fds)); + return (execute_builtin(cmd, app, type)); + return (exec_external_cmd(cmd, app)); } diff --git a/src/executing/simple_cmd/subprocess.c b/src/executing/simple_cmd/subprocess.c index 0590ea4..e34ab7a 100644 --- a/src/executing/simple_cmd/subprocess.c +++ b/src/executing/simple_cmd/subprocess.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/02 18:19:23 by kcolin #+# #+# */ -/* Updated: 2025/04/29 15:14:32 by kcolin ### ########.fr */ +/* Created: 2025/05/06 14:50:26 by kcolin #+# #+# */ +/* Updated: 2025/05/06 14:50:26 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,9 +72,8 @@ static int ft_execve(char *exe, char **argv, char **envp) } t_subprocess execute_subprocess(char *exe, t_simple_cmd *cmd, - t_minishell *app, t_std_fds *fds) + t_minishell *app) { - std_fds_close(fds); app->last_return_value = ft_execve(exe, argv_from_wordlist(cmd->words), envp_from_env(app->env)); return (SUBPROCESS); diff --git a/src/executing/simple_cmd/subprocess.h b/src/executing/simple_cmd/subprocess.h index 2e8fd48..f32a576 100644 --- a/src/executing/simple_cmd/subprocess.h +++ b/src/executing/simple_cmd/subprocess.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/02 18:17:48 by kcolin #+# #+# */ -/* Updated: 2025/04/29 15:14:09 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 14:50:33 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,6 @@ # include "../../minishell.h" t_subprocess execute_subprocess(char *exe, t_simple_cmd *cmd, - t_minishell *app, t_std_fds *fds); + t_minishell *app); #endif // SUBPROCESS_H