From 9271b7fa928a55157d445679b4b988a39035ce7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 16 Apr 2025 18:23:06 +0200 Subject: [PATCH] fix(parsing/pipeline): leak when optional pipeline returns NULL --- src/parser/group_cmd/group_cmd_parse.c | 10 +++++++--- src/parser/pipeline/pipeline_parse.c | 7 ++++--- test.sh | 7 +++++++ 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/parser/group_cmd/group_cmd_parse.c b/src/parser/group_cmd/group_cmd_parse.c index 63ec22b..2f085a9 100644 --- a/src/parser/group_cmd/group_cmd_parse.c +++ b/src/parser/group_cmd/group_cmd_parse.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:46:28 by khais #+# #+# */ -/* Updated: 2025/04/16 15:43:44 by khais ### ########.fr */ +/* Updated: 2025/04/17 12:27:05 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ #include "../redirect/redirect_parse.h" #include "../simple_cmd/simple_cmd_parse.h" #include "../cmd/cmds_parse.h" +#include "../cmd/cmd_debug.h" t_cmd *minishell_group_cmd_parse(t_minishell *app, t_wordlist **tokens) { @@ -41,11 +42,14 @@ t_cmd *minishell_group_cmd_parse(t_minishell *app, t_wordlist **tokens) t_cmd *minishell_group_or_simple_parse(t_minishell *app, t_wordlist **tokens) { + t_cmd *cmd; + if ((*tokens)->word->token_type == OPEN_PARENTH_TOKEN) { worddesc_destroy(wordlist_pop(tokens)); - return (minishell_group_cmd_parse(app, tokens)); + cmd = minishell_group_cmd_parse(app, tokens); } else - return (minishell_simple_cmd_parse(app, tokens)); + cmd = minishell_simple_cmd_parse(app, tokens); + return (cmd); } diff --git a/src/parser/pipeline/pipeline_parse.c b/src/parser/pipeline/pipeline_parse.c index 76d0fca..ea13f73 100644 --- a/src/parser/pipeline/pipeline_parse.c +++ b/src/parser/pipeline/pipeline_parse.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 11:35:08 by khais #+# #+# */ -/* Updated: 2025/04/16 13:07:51 by khais ### ########.fr */ +/* Updated: 2025/04/18 09:19:12 by khais ### ########.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 (NULL); + return (cmd_destroy(subtree), NULL); if (!opt) return (subtree); while (opt) @@ -36,7 +36,8 @@ 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 (ft_perror("minishell_pipeline_parse"), NULL); + return (cmd_destroy(subtree), + ft_perror("minishell_pipeline_parse"), NULL); } opt = minishell_optional_pipeline_parse(app, tokens); } diff --git a/test.sh b/test.sh index 45fb30e..7123840 100755 --- a/test.sh +++ b/test.sh @@ -697,4 +697,11 @@ expecting <