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 1/3] 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 From fb92167494d811e00c1717be48eac9815b099323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 6 May 2025 16:53:54 +0200 Subject: [PATCH 2/3] fix(here_doc): correctly delete here_doc files in more cases --- src/parser/cmd/cmd_destroy.c | 9 +++++---- src/parser/cmd/cmd_destroy.h | 4 ++-- src/parser/redirect/redirect_from_words.c | 4 ++-- src/parser/redirect/redirect_parse.c | 7 ++++--- src/parser/remove_quotes/cmdgroup_remove_quotes.c | 12 ++++++------ src/parser/simple_cmd/simple_cmd.c | 8 ++++---- src/postprocess/expansion/expand_vars.c | 8 ++++---- src/postprocess/expansion/expand_wildcard.c | 14 +++++++------- src/postprocess/fieldsplit/redirect_fieldsplit.c | 8 ++++---- src/sig/sig_handlers.c | 8 ++++---- 10 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/parser/cmd/cmd_destroy.c b/src/parser/cmd/cmd_destroy.c index 3b34eb8..1b35ac1 100644 --- a/src/parser/cmd/cmd_destroy.c +++ b/src/parser/cmd/cmd_destroy.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/09 16:53:02 by kcolin #+# #+# */ -/* Updated: 2025/05/05 11:22:40 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 16:51:04 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ #include "../simple_cmd/simple_cmd.h" #include "libft.h" -void redirect_destroy(t_redirect *redirect) +void redirect_destroy(t_redirect *redirect, bool delete_file) { t_redirect *next; @@ -25,7 +25,8 @@ void redirect_destroy(t_redirect *redirect) free(redirect->here_doc_eof); if (redirect->type == FT_HEREDOC && redirect->redirectee.filename != NULL - && redirect->redirectee.filename->word != NULL) + && redirect->redirectee.filename->word != NULL + && delete_file) unlink(redirect->redirectee.filename->word); worddesc_destroy(redirect->redirectee.filename); free(redirect->unexpanded_filename); @@ -48,7 +49,7 @@ static void group_cmd_destroy(t_group_cmd *cmd) if (cmd == NULL) return ; cmd_destroy(cmd->cmd); - redirect_destroy(cmd->redirects); + redirect_destroy(cmd->redirects, false); free(cmd); } diff --git a/src/parser/cmd/cmd_destroy.h b/src/parser/cmd/cmd_destroy.h index 8085785..04a5eae 100644 --- a/src/parser/cmd/cmd_destroy.h +++ b/src/parser/cmd/cmd_destroy.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/09 16:52:31 by kcolin #+# #+# */ -/* Updated: 2025/04/14 15:10:13 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 16:50:26 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,6 @@ # include "../../minishell.h" void cmd_destroy(t_cmd *cmd); -void redirect_destroy(t_redirect *redirect); +void redirect_destroy(t_redirect *redirect, bool delete_file); #endif // CMD_DESTROY_H diff --git a/src/parser/redirect/redirect_from_words.c b/src/parser/redirect/redirect_from_words.c index 7cab2f6..30ffe9e 100644 --- a/src/parser/redirect/redirect_from_words.c +++ b/src/parser/redirect/redirect_from_words.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/02 14:40:34 by kcolin #+# #+# */ -/* Updated: 2025/05/02 14:40:34 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 16:50:52 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -83,7 +83,7 @@ t_redirect *redir_from_words(t_worddesc *operator, here_doc(spec, STDIN_FILENO, app), 0, NULL, WORD_TOKEN); worddesc_destroy(spec); if (redir->redirectee.filename == NULL) - return (redirect_destroy(redir), NULL); + return (redirect_destroy(redir, true), NULL); } else redir->redirectee.filename = specifier; diff --git a/src/parser/redirect/redirect_parse.c b/src/parser/redirect/redirect_parse.c index 528b8cf..8b8c3be 100644 --- a/src/parser/redirect/redirect_parse.c +++ b/src/parser/redirect/redirect_parse.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:13:58 by kcolin #+# #+# */ -/* Updated: 2025/04/17 11:45:24 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 16:57:51 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,11 +34,12 @@ t_redirect *minishell_redirect_parse(t_minishell *app, t_wordlist **tokens) else { worddesc_destroy(redir_operator); - return (ft_errno(FT_EERRNO), redirect_destroy(redir_list), NULL); + return (ft_errno(FT_EERRNO), redirect_destroy(redir_list, true), + NULL); } new_redir = redir_from_words(redir_operator, redir_specifier, app); if (new_redir == NULL && ft_errno_get() != FT_ESUCCESS) - return (redirect_destroy(redir_list), NULL); + return (redirect_destroy(redir_list, true), NULL); t_redirect_add_back(&redir_list, new_redir); } return (redir_list); diff --git a/src/parser/remove_quotes/cmdgroup_remove_quotes.c b/src/parser/remove_quotes/cmdgroup_remove_quotes.c index 9ad039c..b865079 100644 --- a/src/parser/remove_quotes/cmdgroup_remove_quotes.c +++ b/src/parser/remove_quotes/cmdgroup_remove_quotes.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* cmdgroup_remove_quotes.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jguelen +#+ +:+ +#+ */ +/* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/03/20 17:36:20 by kcolin #+# #+# */ -/* Updated: 2025/05/02 12:53:03 by jguelen ### ########.fr */ +/* Created: 2025/05/06 16:50:43 by kcolin #+# #+# */ +/* Updated: 2025/05/06 16:50:43 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,10 +33,10 @@ static t_redirect *redirection_remove_quotes(t_redirect *in_list) current->redirectee.filename = result; if (current->redirectee.filename == NULL) { - redirect_destroy(in_list); - redirect_destroy(out_list); + redirect_destroy(in_list, true); + redirect_destroy(out_list, true); ft_errno(FT_EERRNO); - return (redirect_destroy(current), NULL); + return (redirect_destroy(current, true), NULL); } } out_list = t_redirect_add_back(&out_list, current); diff --git a/src/parser/simple_cmd/simple_cmd.c b/src/parser/simple_cmd/simple_cmd.c index 77c897b..8d146fa 100644 --- a/src/parser/simple_cmd/simple_cmd.c +++ b/src/parser/simple_cmd/simple_cmd.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* simple_cmd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jguelen +#+ +:+ +#+ */ +/* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/21 12:30:07 by kcolin #+# #+# */ -/* Updated: 2025/05/02 12:53:31 by jguelen ### ########.fr */ +/* Created: 2025/05/06 16:52:05 by kcolin #+# #+# */ +/* Updated: 2025/05/06 16:52:05 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ void simple_cmd_destroy(t_simple_cmd *cmd) if (cmd == NULL) return ; wordlist_destroy(cmd->words); - redirect_destroy(cmd->redirections); + redirect_destroy(cmd->redirections, false); free(cmd); } diff --git a/src/postprocess/expansion/expand_vars.c b/src/postprocess/expansion/expand_vars.c index eb3e5c9..6f77edf 100644 --- a/src/postprocess/expansion/expand_vars.c +++ b/src/postprocess/expansion/expand_vars.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/01 13:34:51 by kcolin #+# #+# */ -/* Updated: 2025/04/28 14:38:29 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 16:52:25 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,9 +36,9 @@ static t_redirect *redirection_var_expansion(t_redirect *in_list, if (current->redirectee.filename == NULL && ft_errno_get() != FT_ESUCCESS) { - redirect_destroy(in_list); - redirect_destroy(out_list); - return (redirect_destroy(current), NULL); + redirect_destroy(in_list, true); + redirect_destroy(out_list, true); + return (redirect_destroy(current, true), NULL); } } out_list = t_redirect_add_back(&out_list, current); diff --git a/src/postprocess/expansion/expand_wildcard.c b/src/postprocess/expansion/expand_wildcard.c index 8ababac..83c2cd1 100644 --- a/src/postprocess/expansion/expand_wildcard.c +++ b/src/postprocess/expansion/expand_wildcard.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* expand_wildcard.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jguelen +#+ +:+ +#+ */ +/* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/03 19:28:28 by kcolin #+# #+# */ -/* Updated: 2025/05/02 13:00:43 by jguelen ### ########.fr */ +/* Created: 2025/05/06 16:51:47 by kcolin #+# #+# */ +/* Updated: 2025/05/06 16:52:34 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,13 +30,13 @@ static t_redirect *redirect_expand_wildcard_one(t_redirect *current) if (expansion_result != NULL) { ambiguous_redirect(current->unexpanded_filename); - redirect_destroy(current); + redirect_destroy(current, true); wordlist_destroy(expansion_result); return (ft_errno(FT_EERRNO), NULL); } } else if (expansion_result == NULL && ft_errno_get() != FT_ESUCCESS) - return (redirect_destroy(current), NULL); + return (redirect_destroy(current, true), NULL); return (current); } @@ -55,8 +55,8 @@ static t_redirect *redirect_expand_wildcards(t_redirect *in_list) if (redirect_expand_wildcard_one(current) == NULL && ft_errno_get() != FT_ESUCCESS) { - redirect_destroy(in_list); - redirect_destroy(out_list); + redirect_destroy(in_list, true); + redirect_destroy(out_list, true); return (NULL); } } diff --git a/src/postprocess/fieldsplit/redirect_fieldsplit.c b/src/postprocess/fieldsplit/redirect_fieldsplit.c index 85c3115..0b5bd3f 100644 --- a/src/postprocess/fieldsplit/redirect_fieldsplit.c +++ b/src/postprocess/fieldsplit/redirect_fieldsplit.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/21 11:54:16 by kcolin #+# #+# */ -/* Updated: 2025/04/23 11:19:26 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 16:51:36 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,9 +20,9 @@ static void redirect_fieldsplit_cleanup(t_redirect *in_list, t_redirect *out_list, t_redirect *current, t_simple_cmd *cmd) { - redirect_destroy(in_list); - redirect_destroy(out_list); - redirect_destroy(current); + redirect_destroy(in_list, true); + redirect_destroy(out_list, true); + redirect_destroy(current, true); cmd->redirections = NULL; } diff --git a/src/sig/sig_handlers.c b/src/sig/sig_handlers.c index f5db905..b466af8 100644 --- a/src/sig/sig_handlers.c +++ b/src/sig/sig_handlers.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* sig_handlers.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jguelen +#+ +:+ +#+ */ +/* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/17 12:01:39 by kcolin #+# #+# */ -/* Updated: 2025/05/05 12:03:03 by jguelen ### ########.fr */ +/* Created: 2025/05/06 16:57:25 by kcolin #+# #+# */ +/* Updated: 2025/05/06 16:57:27 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,4 +70,4 @@ void sig_heredoc(int signum, siginfo_t *siginfo, void *context) ft_printf("\n"); } g_signum = signum; -} \ No newline at end of file +} From 38dfe3c084108d2bcbfb7afbe60950c895ced8e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 6 May 2025 17:20:44 +0200 Subject: [PATCH 3/3] fix(here_doc): correctly delete here_doc files in all(?) cases --- .../simple_cmd/handle_redirections.c | 8 ++++++- src/minishell.c | 8 +++---- src/parser/cmd/cmd_destroy.c | 22 +++++++++---------- src/parser/cmd/cmd_destroy.h | 4 ++-- src/parser/cmd/cmds_parse.c | 10 ++++----- src/parser/cmd_parsing.c | 8 +++---- .../connec_cmd/connec_reorient_subtree.c | 10 ++++----- src/parser/group_cmd/group_cmd_parse.c | 10 ++++----- src/parser/pipeline/pipeline_parse.c | 8 +++---- src/parser/simple_cmd/simple_cmd.c | 6 ++--- src/parser/simple_cmd/simple_cmd.h | 4 ++-- src/parser/simple_cmd/simple_cmd_parse.c | 10 ++++----- 12 files changed, 57 insertions(+), 51 deletions(-) diff --git a/src/executing/simple_cmd/handle_redirections.c b/src/executing/simple_cmd/handle_redirections.c index 2a3f6eb..124a7d4 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/05/06 14:23:45 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 17:15:25 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,6 +48,12 @@ t_minishell *handle_redirections(t_redirect *redirections, t_minishell *app) if (do_redirection(redirections) == NULL) { app->last_return_value = 1; + while (redirections != NULL) + { + if (redirections->type == FT_HEREDOC) + unlink(redirections->redirectee.filename->word); + redirections = redirections->next; + } return (NULL); } redirections = redirections->next; diff --git a/src/minishell.c b/src/minishell.c index 30f9021..41c854f 100644 --- a/src/minishell.c +++ b/src/minishell.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* minishell.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jguelen +#+ +:+ +#+ */ +/* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/05/02 11:53:53 by kcolin #+# #+# */ -/* Updated: 2025/05/02 17:38:40 by jguelen ### ########.fr */ +/* Created: 2025/05/06 17:12:03 by kcolin #+# #+# */ +/* Updated: 2025/05/06 17:12:03 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,7 +74,7 @@ int main(int argc, char *argv[], char **envp) free(line); debug_command(cmd, &app); retvalue = execute_command(cmd, &app); - cmd_destroy(cmd); + cmd_destroy(cmd, false); if (retvalue == SUBPROCESS) break ; line = get_command(&app); diff --git a/src/parser/cmd/cmd_destroy.c b/src/parser/cmd/cmd_destroy.c index 1b35ac1..1d81840 100644 --- a/src/parser/cmd/cmd_destroy.c +++ b/src/parser/cmd/cmd_destroy.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/09 16:53:02 by kcolin #+# #+# */ -/* Updated: 2025/05/06 16:51:04 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 17:10:21 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,33 +35,33 @@ void redirect_destroy(t_redirect *redirect, bool delete_file) } } -static void connec_cmd_destroy(t_connec_cmd *cmd) +static void connec_cmd_destroy(t_connec_cmd *cmd, bool delete_file) { if (cmd == NULL) return ; - cmd_destroy(cmd->first); - cmd_destroy(cmd->second); + cmd_destroy(cmd->first, delete_file); + cmd_destroy(cmd->second, delete_file); free(cmd); } -static void group_cmd_destroy(t_group_cmd *cmd) +static void group_cmd_destroy(t_group_cmd *cmd, bool delete_file) { if (cmd == NULL) return ; - cmd_destroy(cmd->cmd); - redirect_destroy(cmd->redirects, false); + cmd_destroy(cmd->cmd, delete_file); + redirect_destroy(cmd->redirects, delete_file); free(cmd); } -void cmd_destroy(t_cmd *cmd) +void cmd_destroy(t_cmd *cmd, bool delete_file) { if (cmd == NULL) return ; if (cmd->type == FT_CONNECTION) - connec_cmd_destroy(cmd->value.connection); + connec_cmd_destroy(cmd->value.connection, delete_file); if (cmd->type == FT_GROUP) - group_cmd_destroy(cmd->value.group); + group_cmd_destroy(cmd->value.group, delete_file); if (cmd->type == FT_SIMPLE) - simple_cmd_destroy(cmd->value.simple); + simple_cmd_destroy(cmd->value.simple, delete_file); free(cmd); } diff --git a/src/parser/cmd/cmd_destroy.h b/src/parser/cmd/cmd_destroy.h index 04a5eae..52752dc 100644 --- a/src/parser/cmd/cmd_destroy.h +++ b/src/parser/cmd/cmd_destroy.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/09 16:52:31 by kcolin #+# #+# */ -/* Updated: 2025/05/06 16:50:26 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 17:08:41 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ # include "../../minishell.h" -void cmd_destroy(t_cmd *cmd); +void cmd_destroy(t_cmd *cmd, bool delete_file); void redirect_destroy(t_redirect *redirect, bool delete_file); #endif // CMD_DESTROY_H diff --git a/src/parser/cmd/cmds_parse.c b/src/parser/cmd/cmds_parse.c index 3956743..c406a5c 100644 --- a/src/parser/cmd/cmds_parse.c +++ b/src/parser/cmd/cmds_parse.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* cmds_parse.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jguelen +#+ +:+ +#+ */ +/* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/04/16 13:11:33 by kcolin #+# #+# */ -/* Updated: 2025/05/02 12:49:54 by jguelen ### ########.fr */ +/* Created: 2025/05/06 17:09:29 by kcolin #+# #+# */ +/* Updated: 2025/05/06 17:10:00 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ t_cmd *minishell_cmds_parse(t_minishell *app, t_wordlist **tokens) return (NULL); opt = minishell_opt_cmds_parse(app, tokens, &connec); if (!opt && ft_errno_get() != FT_ESUCCESS) - return (cmd_destroy(subtree), NULL); + return (cmd_destroy(subtree, true), NULL); if (!opt) return (subtree); while (opt) @@ -42,6 +42,6 @@ t_cmd *minishell_cmds_parse(t_minishell *app, t_wordlist **tokens) opt = minishell_opt_cmds_parse(app, tokens, &connec); } if (ft_errno_get() != FT_ESUCCESS) - return (cmd_destroy(subtree), NULL); + return (cmd_destroy(subtree, true), NULL); return (list); } diff --git a/src/parser/cmd_parsing.c b/src/parser/cmd_parsing.c index ecf1408..2d36fc2 100644 --- a/src/parser/cmd_parsing.c +++ b/src/parser/cmd_parsing.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* cmd_parsing.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jguelen +#+ +:+ +#+ */ +/* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/03/31 10:28:28 by jguelen #+# #+# */ -/* Updated: 2025/05/02 13:00:27 by jguelen ### ########.fr */ +/* Created: 2025/05/06 17:10:37 by kcolin #+# #+# */ +/* Updated: 2025/05/06 17:10:43 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,7 +69,7 @@ t_cmd *minishell_parse(t_minishell *app, char *command_line) else parse_error(app, tokens->word); wordlist_destroy(tokens); - cmd_destroy(root_cmd); + cmd_destroy(root_cmd, true); return (NULL); } return (root_cmd); diff --git a/src/parser/connec_cmd/connec_reorient_subtree.c b/src/parser/connec_cmd/connec_reorient_subtree.c index 1b0eee0..d4ec125 100644 --- a/src/parser/connec_cmd/connec_reorient_subtree.c +++ b/src/parser/connec_cmd/connec_reorient_subtree.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 11:37:35 by kcolin #+# #+# */ -/* Updated: 2025/04/15 11:39:41 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 17:10:36 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,16 +21,16 @@ t_cmd *connec_reorient_subtree(t_cmd **list, t_cmd **subtree, *list = cmd_create(FT_CONNECTION); if (!*list) { - cmd_destroy(*subtree); - cmd_destroy(*opt); + cmd_destroy(*subtree, true); + cmd_destroy(*opt, true); return (NULL); } (*list)->value.connection = ft_calloc(1, sizeof(t_connec_cmd)); if (!(*list)->value.connection) { ft_errno(FT_ENOMEM); - cmd_destroy(*subtree); - cmd_destroy(*opt); + cmd_destroy(*subtree, true); + cmd_destroy(*opt, true); return (free(*list), NULL); } (*list)->type = FT_CONNECTION; diff --git a/src/parser/group_cmd/group_cmd_parse.c b/src/parser/group_cmd/group_cmd_parse.c index a3d1e75..93f20c4 100644 --- a/src/parser/group_cmd/group_cmd_parse.c +++ b/src/parser/group_cmd/group_cmd_parse.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:46:28 by kcolin #+# #+# */ -/* Updated: 2025/04/28 14:26:10 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 17:11:19 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,19 +26,19 @@ t_cmd *minishell_group_cmd_parse(t_minishell *app, t_wordlist **tokens) subtree = minishell_cmds_parse(app, tokens); if (!subtree || (*tokens) == NULL || (*tokens)->word->token_type != CLOSE_PARENTH_TOKEN) - return (cmd_destroy(subtree), ft_errno(FT_EERRNO), NULL); + return (cmd_destroy(subtree, true), ft_errno(FT_EERRNO), NULL); worddesc_destroy(wordlist_pop(tokens)); group = cmd_create(FT_GROUP); if (!group) - return (cmd_destroy(subtree), NULL); + return (cmd_destroy(subtree, true), NULL); group->value.group = ft_calloc(1, sizeof(t_group_cmd)); if (!group->value.group) - return (cmd_destroy(subtree), NULL); + return (cmd_destroy(subtree, true), NULL); group->value.group->cmd = subtree; group->value.group->redirects = minishell_redirect_parse(app, tokens); if (group->value.group->redirects == NULL && ft_errno_get() != FT_ESUCCESS) - return (cmd_destroy(group), NULL); + return (cmd_destroy(group, true), NULL); return (group); } diff --git a/src/parser/pipeline/pipeline_parse.c b/src/parser/pipeline/pipeline_parse.c index 144f89b..0c498d0 100644 --- a/src/parser/pipeline/pipeline_parse.c +++ b/src/parser/pipeline/pipeline_parse.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 11:35:08 by kcolin #+# #+# */ -/* Updated: 2025/04/18 09:19:12 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 17:10:11 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ t_cmd *minishell_pipeline_parse(t_minishell *app, t_wordlist **tokens) return (NULL); opt = minishell_optional_pipeline_parse(app, tokens); if (!opt && ft_errno_get() != FT_ESUCCESS) - return (cmd_destroy(subtree), NULL); + return (cmd_destroy(subtree, true), NULL); if (!opt) return (subtree); while (opt) @@ -36,12 +36,12 @@ t_cmd *minishell_pipeline_parse(t_minishell *app, t_wordlist **tokens) if (connec_reorient_subtree(&pipeline, &subtree, &opt, FT_PIPE) == NULL) { app->last_return_value = 1; - return (cmd_destroy(subtree), + return (cmd_destroy(subtree, true), ft_perror("minishell_pipeline_parse"), NULL); } opt = minishell_optional_pipeline_parse(app, tokens); } if (ft_errno_get() != FT_ESUCCESS) - return (cmd_destroy(subtree), NULL); + return (cmd_destroy(subtree, true), NULL); return (pipeline); } diff --git a/src/parser/simple_cmd/simple_cmd.c b/src/parser/simple_cmd/simple_cmd.c index 8d146fa..84c4f26 100644 --- a/src/parser/simple_cmd/simple_cmd.c +++ b/src/parser/simple_cmd/simple_cmd.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/05/06 16:52:05 by kcolin #+# #+# */ -/* Updated: 2025/05/06 16:52:05 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 17:07:05 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,12 +27,12 @@ t_simple_cmd *simple_cmd_from_wordlist(t_wordlist *words) return (cmd); } -void simple_cmd_destroy(t_simple_cmd *cmd) +void simple_cmd_destroy(t_simple_cmd *cmd, bool delete_file) { if (cmd == NULL) return ; wordlist_destroy(cmd->words); - redirect_destroy(cmd->redirections, false); + redirect_destroy(cmd->redirections, delete_file); free(cmd); } diff --git a/src/parser/simple_cmd/simple_cmd.h b/src/parser/simple_cmd/simple_cmd.h index 88f7930..fda17db 100644 --- a/src/parser/simple_cmd/simple_cmd.h +++ b/src/parser/simple_cmd/simple_cmd.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/21 12:24:57 by kcolin #+# #+# */ -/* Updated: 2025/04/10 15:33:10 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 17:11:01 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ # include "../../minishell.h" t_simple_cmd *simple_cmd_from_wordlist(t_wordlist *words); -void simple_cmd_destroy(t_simple_cmd *cmd); +void simple_cmd_destroy(t_simple_cmd *cmd, bool delete_file); void simple_cmd_debug(t_simple_cmd *cmd, t_buffer *leader, bool is_last); void simple_cmd_root_debug(t_simple_cmd *cmd); diff --git a/src/parser/simple_cmd/simple_cmd_parse.c b/src/parser/simple_cmd/simple_cmd_parse.c index bf33a90..1aa8d00 100644 --- a/src/parser/simple_cmd/simple_cmd_parse.c +++ b/src/parser/simple_cmd/simple_cmd_parse.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:38:47 by kcolin #+# #+# */ -/* Updated: 2025/04/28 14:30:35 by kcolin ### ########.fr */ +/* Updated: 2025/05/06 17:11:41 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,19 +41,19 @@ t_cmd *minishell_simple_cmd_parse(t_minishell *app, t_wordlist **tokens) redir = minishell_redirect_parse(app, tokens); t_redirect_add_back(&simple->value.simple->redirections, redir); if (ft_errno_get() != FT_ESUCCESS) - return (cmd_destroy(simple), NULL); + return (cmd_destroy(simple, true), NULL); while ((*tokens) && (*tokens)->word->token_type == WORD_TOKEN) { simple->value.simple->words = wordlist_push(simple->value.simple->words, wordlist_pop(tokens)); if (!simple->value.simple->words) - return (ft_errno(FT_EERRNO), cmd_destroy(simple), NULL); + return (ft_errno(FT_EERRNO), cmd_destroy(simple, true), NULL); redir = minishell_redirect_parse(app, tokens); if (redir == NULL && ft_errno_get() != FT_ESUCCESS) - return (cmd_destroy(simple), NULL); + return (cmd_destroy(simple, true), NULL); t_redirect_add_back(&simple->value.simple->redirections, redir); } if (!simple->value.simple->words && !simple->value.simple->redirections) - return (cmd_destroy(simple), ft_errno(FT_EERRNO), NULL); + return (cmd_destroy(simple, true), ft_errno(FT_EERRNO), NULL); return (simple); }