From 5716c4b3dca28c6921fc2bf912be8730b69979d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 15 Apr 2025 14:10:03 +0200 Subject: [PATCH] parsing: pass correct arguments to prevent use-after-free --- src/minishell.c | 13 +++++++++++- src/parser/cmd_parsing.c | 20 ++++++++++--------- src/parser/cmd_parsing.h | 8 ++++---- src/parser/group_cmd/group_cmd_parse.c | 12 +++++------ src/parser/group_cmd/group_cmd_parse.h | 4 ++-- src/parser/pipeline/optional_pipeline_parse.c | 11 ++++++---- src/parser/pipeline/optional_pipeline_parse.h | 4 ++-- src/parser/pipeline/pipeline_parse.c | 4 ++-- src/parser/pipeline/pipeline_parse.h | 4 ++-- src/parser/redirect/redirect_parse.c | 14 +++++++------ src/parser/redirect/redirect_parse.h | 4 ++-- src/parser/simple_cmd/simple_cmd_parse.c | 10 +++++----- src/parser/simple_cmd/simple_cmd_parse.h | 4 ++-- 13 files changed, 65 insertions(+), 47 deletions(-) diff --git a/src/minishell.c b/src/minishell.c index 27ddc99..b706ffb 100644 --- a/src/minishell.c +++ b/src/minishell.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */ -/* Updated: 2025/04/15 11:54:46 by khais ### ########.fr */ +/* Updated: 2025/04/15 11:56:50 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ #include "postprocess/fieldsplit/fieldsplit.h" #include "postprocess/expansion/expand_wildcard.h" #include "sig/sig.h" +#include "parser/cmd/cmd_debug.h" /* ** execute command @@ -61,6 +62,15 @@ static void app_init(t_minishell *app, char **envp) app->env = env_from_envp(envp); } +static void debug_command(t_cmd *cmd) +{ + t_buffer *indent; + + indent = ft_buffer_new(); + cmd_debug(cmd, indent, true); + ft_buffer_free(indent); +} + int main(int argc, char *argv[], char **envp) { char *line; @@ -73,6 +83,7 @@ int main(int argc, char *argv[], char **envp) app_init(&app, envp); line = "echo coucou"; cmd = minishell_parse(&app, line); + debug_command(cmd); cmd_destroy(cmd); env_destroy(app.env); return (0); diff --git a/src/parser/cmd_parsing.c b/src/parser/cmd_parsing.c index dec239d..e992230 100644 --- a/src/parser/cmd_parsing.c +++ b/src/parser/cmd_parsing.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/31 10:28:28 by jguelen #+# #+# */ -/* Updated: 2025/04/15 11:38:55 by khais ### ########.fr */ +/* Updated: 2025/04/15 14:15:49 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,31 +28,33 @@ void parse_error(t_minishell *app, t_worddesc *token) app->last_return_value = 2; } -t_cmd *minishell_group_or_simple_parse(t_minishell *app, t_wordlist *tokens) +t_cmd *minishell_group_or_simple_parse(t_minishell *app, t_wordlist **tokens) { - if (tokens->word->token_type == OPEN_PARENTH_TOKEN) + if ((*tokens)->word->token_type == OPEN_PARENTH_TOKEN) { - worddesc_destroy(wordlist_pop(&tokens)); + worddesc_destroy(wordlist_pop(tokens)); return (minishell_group_cmd_parse(app, tokens)); } else return (minishell_simple_cmd_parse(app, tokens)); } -t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_wordlist *tokens, +t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_wordlist **tokens, t_connector *connec) { t_worddesc *token; t_cmd *opt; - token = tokens->word; + if ((*tokens) == NULL) + return (NULL); + token = (*tokens)->word; if (token->token_type == OR_TOKEN || token->token_type == AND_TOKEN) { if (token->token_type == OR_TOKEN) *connec = FT_OR; else *connec = FT_AND; - token = wordlist_pop(&tokens); + token = wordlist_pop(tokens); worddesc_destroy(token); opt = minishell_pipeline_parse(app, tokens); if (!opt) @@ -65,7 +67,7 @@ t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_wordlist *tokens, /* ** Parse list of commands or pipeline. */ -t_cmd *minishell_cmds_parse(t_minishell *app, t_wordlist *tokens) +t_cmd *minishell_cmds_parse(t_minishell *app, t_wordlist **tokens) { t_cmd *subtree; t_cmd *opt; @@ -109,7 +111,7 @@ t_cmd *minishell_parse(t_minishell *app, char *command_line) tokens = minishell_wordsplit(command_line); if (!tokens) return (NULL); - root_cmd = minishell_cmds_parse(app, tokens); + root_cmd = minishell_cmds_parse(app, &tokens); if (!root_cmd) { if (ft_errno_get() != FT_ESUCCESS && !app->last_return_value) diff --git a/src/parser/cmd_parsing.h b/src/parser/cmd_parsing.h index afb3851..e0cc50a 100644 --- a/src/parser/cmd_parsing.h +++ b/src/parser/cmd_parsing.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:14:13 by khais #+# #+# */ -/* Updated: 2025/04/15 11:35:26 by khais ### ########.fr */ +/* Updated: 2025/04/15 14:07:34 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,10 +21,10 @@ void parse_error(t_minishell *app, t_worddesc *token); t_redirect *t_redirect_add_back(t_redirect **init, t_redirect *back); t_cmd *minishell_parse(t_minishell *app, char *command_line); -t_cmd *minishell_cmds_parse(t_minishell *app, t_wordlist *tokens); -t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_wordlist *tokens, +t_cmd *minishell_cmds_parse(t_minishell *app, t_wordlist **tokens); +t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_wordlist **tokens, t_connector *connec); t_cmd *minishell_group_or_simple_parse(t_minishell *app, - t_wordlist *tokens); + t_wordlist **tokens); #endif diff --git a/src/parser/group_cmd/group_cmd_parse.c b/src/parser/group_cmd/group_cmd_parse.c index a83b964..5c7c4c2 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/15 10:47:19 by khais ### ########.fr */ +/* Updated: 2025/04/15 14:08:40 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,21 +16,21 @@ #include "../cmd/cmd.h" #include "../redirect/redirect_parse.h" -t_cmd *minishell_group_cmd_parse(t_minishell *app, t_wordlist *tokens) +t_cmd *minishell_group_cmd_parse(t_minishell *app, t_wordlist **tokens) { t_cmd *subtree; t_cmd *group; subtree = minishell_cmds_parse(app, tokens); if (!subtree - || tokens->word->token_type != CLOSE_PARENTH_TOKEN) + || (*tokens)->word->token_type != CLOSE_PARENTH_TOKEN) { ft_errno(FT_EERRNO); - if (tokens->word->token_type != CLOSE_PARENTH_TOKEN) - parse_error(app, tokens->word); + if ((*tokens)->word->token_type != CLOSE_PARENTH_TOKEN) + parse_error(app, (*tokens)->word); return (cmd_destroy(subtree), NULL); } - worddesc_destroy(wordlist_pop(&tokens)); + worddesc_destroy(wordlist_pop(tokens)); group = cmd_create(FT_GROUP); if (!group) return (cmd_destroy(subtree), NULL); diff --git a/src/parser/group_cmd/group_cmd_parse.h b/src/parser/group_cmd/group_cmd_parse.h index eba1731..bbf0876 100644 --- a/src/parser/group_cmd/group_cmd_parse.h +++ b/src/parser/group_cmd/group_cmd_parse.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:45:49 by khais #+# #+# */ -/* Updated: 2025/04/15 10:46:15 by khais ### ########.fr */ +/* Updated: 2025/04/15 14:06:54 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,6 @@ # include "../../minishell.h" -t_cmd *minishell_group_cmd_parse(t_minishell *app, t_wordlist *tokens); +t_cmd *minishell_group_cmd_parse(t_minishell *app, t_wordlist **tokens); #endif // GROUP_CMD_PARSE_H diff --git a/src/parser/pipeline/optional_pipeline_parse.c b/src/parser/pipeline/optional_pipeline_parse.c index 45fdcf8..0f9987c 100644 --- a/src/parser/pipeline/optional_pipeline_parse.c +++ b/src/parser/pipeline/optional_pipeline_parse.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:43:04 by khais #+# #+# */ -/* Updated: 2025/04/15 10:43:40 by khais ### ########.fr */ +/* Updated: 2025/04/15 14:15:18 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,15 +14,18 @@ #include "../../ft_errno.h" #include "../cmd_parsing.h" -t_cmd *minishell_optional_pipeline_parse(t_minishell *app, t_wordlist *tokens) +t_cmd *minishell_optional_pipeline_parse(t_minishell *app, + t_wordlist **tokens) { t_cmd *opt; t_worddesc *token; - token = tokens->word; + if ((*tokens) == NULL) + return (NULL); + token = (*tokens)->word; if (token->token_type == PIPE_TOKEN) { - token = wordlist_pop(&tokens); + token = wordlist_pop(tokens); worddesc_destroy(token); opt = minishell_group_or_simple_parse(app, tokens); if (!opt) diff --git a/src/parser/pipeline/optional_pipeline_parse.h b/src/parser/pipeline/optional_pipeline_parse.h index 7de7ed6..ef2c7a5 100644 --- a/src/parser/pipeline/optional_pipeline_parse.h +++ b/src/parser/pipeline/optional_pipeline_parse.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:42:34 by khais #+# #+# */ -/* Updated: 2025/04/15 10:42:57 by khais ### ########.fr */ +/* Updated: 2025/04/15 14:07:55 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,6 @@ # include "../../minishell.h" t_cmd *minishell_optional_pipeline_parse(t_minishell *app, - t_wordlist *tokens); + t_wordlist **tokens); #endif // OPTIONAL_PIPELINE_PARSE_H diff --git a/src/parser/pipeline/pipeline_parse.c b/src/parser/pipeline/pipeline_parse.c index 4ff4a09..80f0466 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/15 11:38:39 by khais ### ########.fr */ +/* Updated: 2025/04/15 14:09:28 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ #include "../cmd/cmd_destroy.h" #include "../connec_cmd/connec_reorient_subtree.h" -t_cmd *minishell_pipeline_parse(t_minishell *app, t_wordlist *tokens) +t_cmd *minishell_pipeline_parse(t_minishell *app, t_wordlist **tokens) { t_cmd *subtree; t_cmd *opt; diff --git a/src/parser/pipeline/pipeline_parse.h b/src/parser/pipeline/pipeline_parse.h index 398933b..ddaae76 100644 --- a/src/parser/pipeline/pipeline_parse.h +++ b/src/parser/pipeline/pipeline_parse.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 11:34:38 by khais #+# #+# */ -/* Updated: 2025/04/15 11:35:00 by khais ### ########.fr */ +/* Updated: 2025/04/15 14:09:33 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,6 @@ # include "../../minishell.h" -t_cmd *minishell_pipeline_parse(t_minishell *app, t_wordlist *tokens); +t_cmd *minishell_pipeline_parse(t_minishell *app, t_wordlist **tokens); #endif // PIPELINE_PARSE_H diff --git a/src/parser/redirect/redirect_parse.c b/src/parser/redirect/redirect_parse.c index b7495e3..6265fc3 100644 --- a/src/parser/redirect/redirect_parse.c +++ b/src/parser/redirect/redirect_parse.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:13:58 by khais #+# #+# */ -/* Updated: 2025/04/15 10:15:08 by khais ### ########.fr */ +/* Updated: 2025/04/15 14:14:07 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,23 +16,25 @@ #include "../../ft_errno.h" #include "../cmd/cmd_destroy.h" -t_redirect *minishell_redirect_parse(t_minishell *app, t_wordlist *tokens) +t_redirect *minishell_redirect_parse(t_minishell *app, t_wordlist **tokens) { t_redirect *redir_list; t_worddesc *redir_operator; t_worddesc *redir_specifier; t_redirect *new_redir; + if ((*tokens) == NULL) + return (NULL); redir_list = NULL; - while (is_redir(tokens->word)) + while (is_redir((*tokens)->word)) { - if (tokens->next == NULL) + if ((*tokens)->next == NULL) { ft_errno(FT_EERRNO); return (redirect_destroy(redir_list), NULL); } - redir_operator = wordlist_pop(&tokens); - redir_specifier = wordlist_pop(&tokens); + redir_operator = wordlist_pop(tokens); + redir_specifier = wordlist_pop(tokens); new_redir = redir_from_words(redir_operator, redir_specifier, app); t_redirect_add_back(&redir_list, new_redir); } diff --git a/src/parser/redirect/redirect_parse.h b/src/parser/redirect/redirect_parse.h index 3428ad4..a8d115f 100644 --- a/src/parser/redirect/redirect_parse.h +++ b/src/parser/redirect/redirect_parse.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:13:28 by khais #+# #+# */ -/* Updated: 2025/04/15 10:14:13 by khais ### ########.fr */ +/* Updated: 2025/04/15 14:06:36 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,6 @@ # include "../../minishell.h" -t_redirect *minishell_redirect_parse(t_minishell *app, t_wordlist *tokens); +t_redirect *minishell_redirect_parse(t_minishell *app, t_wordlist **tokens); #endif // REDIRECT_PARSE_H diff --git a/src/parser/simple_cmd/simple_cmd_parse.c b/src/parser/simple_cmd/simple_cmd_parse.c index 482be01..616b712 100644 --- a/src/parser/simple_cmd/simple_cmd_parse.c +++ b/src/parser/simple_cmd/simple_cmd_parse.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:38:47 by khais #+# #+# */ -/* Updated: 2025/04/15 10:40:08 by khais ### ########.fr */ +/* Updated: 2025/04/15 14:15:02 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ #include "../cmd/cmd_destroy.h" #include "../cmd_parsing.h" -t_cmd *minishell_simple_cmd_parse(t_minishell *app, t_wordlist *tokens) +t_cmd *minishell_simple_cmd_parse(t_minishell *app, t_wordlist **tokens) { t_cmd *simple; t_redirect *redir; @@ -32,16 +32,16 @@ t_cmd *minishell_simple_cmd_parse(t_minishell *app, t_wordlist *tokens) t_redirect_add_back(&simple->value.simple->redirections, redir); if (ft_errno_get() != FT_ESUCCESS) return (cmd_destroy(simple), NULL); - while (tokens && tokens->word->token_type == WORD_TOKEN) + while ((*tokens) && (*tokens)->word->token_type == WORD_TOKEN) { simple->value.simple->words = wordlist_push(simple->value.simple->words, - wordlist_pop(&tokens)); + wordlist_pop(tokens)); if (!simple->value.simple->words) return (ft_errno(FT_EERRNO), cmd_destroy(simple), NULL); redir = minishell_redirect_parse(app, tokens); t_redirect_add_back(&simple->value.simple->redirections, redir); } if (!simple->value.simple->words) - return (cmd_destroy(simple), parse_error(app, tokens->word), NULL); + return (cmd_destroy(simple), parse_error(app, (*tokens)->word), NULL); return (simple); } diff --git a/src/parser/simple_cmd/simple_cmd_parse.h b/src/parser/simple_cmd/simple_cmd_parse.h index 7cd3eeb..6fa4dad 100644 --- a/src/parser/simple_cmd/simple_cmd_parse.h +++ b/src/parser/simple_cmd/simple_cmd_parse.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 10:38:23 by khais #+# #+# */ -/* Updated: 2025/04/15 10:38:41 by khais ### ########.fr */ +/* Updated: 2025/04/15 14:05:40 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,6 @@ # include "../../minishell.h" -t_cmd *minishell_simple_cmd_parse(t_minishell *app, t_wordlist *tokens); +t_cmd *minishell_simple_cmd_parse(t_minishell *app, t_wordlist **tokens); #endif // SIMPLE_CMD_PARSE_H