diff --git a/src/parser/command_list/command_list.c b/src/parser/command_list/command_list.c index ba8b778..7763987 100644 --- a/src/parser/command_list/command_list.c +++ b/src/parser/command_list/command_list.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/24 17:49:46 by khais #+# #+# */ -/* Updated: 2025/03/04 13:48:37 by khais ### ########.fr */ +/* Updated: 2025/03/04 13:48:56 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -86,11 +86,17 @@ static void cmdlist_builder_delimit( /* ** Create a new command list from the given wordlist. +** +** Makes a copy of the given wordlist */ -t_cmdlist *cmdlist_from_wordlist(t_wordlist *words) +t_cmdlist *cmdlist_from_wordlist(const t_wordlist *wordlist) { t_cmdlist_builder builder; + t_wordlist *words; + words = wordlist_copy(wordlist); + if (words == NULL) + return (NULL); if (setup_cmdlist_builder(&builder, &words) == NULL) return (NULL); while (builder.error == false && builder.current_word != NULL) diff --git a/src/parser/command_list/command_list.h b/src/parser/command_list/command_list.h index 3b2b5c7..b9fa217 100644 --- a/src/parser/command_list/command_list.h +++ b/src/parser/command_list/command_list.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/24 17:45:01 by khais #+# #+# */ -/* Updated: 2025/02/26 13:50:46 by khais ### ########.fr */ +/* Updated: 2025/03/04 13:25:44 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -83,7 +83,7 @@ typedef struct s_cmdlist t_operator *operators; } t_cmdlist; -t_cmdlist *cmdlist_from_wordlist(t_wordlist *words); +t_cmdlist *cmdlist_from_wordlist(const t_wordlist *wordlist); void cmdlist_destroy(t_cmdlist *cmd); #endif // COMMAND_LIST_H diff --git a/tests/cmdlist_use_after_free.c b/tests/cmdlist_use_after_free.c index 3f9462a..b284468 100644 --- a/tests/cmdlist_use_after_free.c +++ b/tests/cmdlist_use_after_free.c @@ -19,6 +19,7 @@ int main(void) { t_wordlist *words = minishell_wordsplit("|"); t_cmdlist *cmd = cmdlist_from_wordlist(words); + wordlist_destroy(words); assert(cmd == NULL); return (0); } diff --git a/tests/parse_command_lists.c b/tests/parse_command_lists.c index 346fc47..a757769 100644 --- a/tests/parse_command_lists.c +++ b/tests/parse_command_lists.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/24 17:40:48 by khais #+# #+# */ -/* Updated: 2025/02/26 17:29:15 by khais ### ########.fr */ +/* Updated: 2025/03/04 13:43:39 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,7 @@ static t_cmdlist *parse_command_list(char *input) ft_dprintf(STDERR_FILENO, "Now checking command list with input [%s]\n", input); t_wordlist *words = minishell_wordsplit(input); t_cmdlist *cmd = cmdlist_from_wordlist(words); + wordlist_destroy(words); return (cmd); }