parsing: pass correct arguments to prevent use-after-free

This commit is contained in:
Khaïs COLIN 2025-04-15 14:10:03 +02:00
parent ba9751e089
commit 5716c4b3dc
13 changed files with 65 additions and 47 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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