From 04eabf096d69a8cf974f6d4bff7e2ea32a03e093 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 30 Apr 2025 14:49:49 +0200 Subject: [PATCH] fix(wildcard): error detection in wordlist_expand_star Will help in closing #167 --- src/parser/wordlist/wordlist_utils.c | 13 +++++++------ src/postprocess/expansion/expand_wildcard.c | 16 +++++++++++++--- src/subst/wildcard_exp.c | 8 ++++---- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/parser/wordlist/wordlist_utils.c b/src/parser/wordlist/wordlist_utils.c index f32153b..9440072 100644 --- a/src/parser/wordlist/wordlist_utils.c +++ b/src/parser/wordlist/wordlist_utils.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* wordlist_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jguelen +#+ +:+ +#+ */ +/* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/10 09:51:34 by jguelen #+# #+# */ -/* Updated: 2025/03/20 14:43:17 by jguelen ### ########.fr */ +/* Updated: 2025/04/30 14:52:32 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #include "../worddesc/worddesc.h" #include #include "../../../libft/libft.h" +#include "../../ft_errno.h" /* ** Creates a new wordlist composed of only one element. Its next field is NULL @@ -27,18 +28,18 @@ t_wordlist *wordlist_independant_create(t_worddesc *word) new = ft_calloc(1, sizeof(t_wordlist)); if (!new) - return (NULL); + return (ft_errno(FT_ENOMEM), NULL); desc_copy = ft_calloc(1, sizeof(t_worddesc)); if (!desc_copy) - return (free(new), NULL); + return (free(new), ft_errno(FT_ENOMEM), NULL); desc_copy->word = ft_strdup(word->word); if (!desc_copy->word) - return (free(desc_copy), free(new), NULL); + return (free(desc_copy), free(new), ft_errno(FT_ENOMEM), NULL); desc_copy->flags = word->flags; desc_copy->marker = ft_strdup(word->marker); new->word = desc_copy; if (!desc_copy->marker && word->marker) - return (wordlist_destroy(new), NULL); + return (wordlist_destroy(new), ft_errno(FT_ENOMEM), NULL); return (new); } diff --git a/src/postprocess/expansion/expand_wildcard.c b/src/postprocess/expansion/expand_wildcard.c index 9b52052..5874835 100644 --- a/src/postprocess/expansion/expand_wildcard.c +++ b/src/postprocess/expansion/expand_wildcard.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 19:28:28 by kcolin #+# #+# */ -/* Updated: 2025/04/21 14:42:41 by kcolin ### ########.fr */ +/* Updated: 2025/04/30 14:51:12 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -60,6 +60,9 @@ static t_redirect *redirect_expand_wildcards(t_redirect *in_list) return (out_list); } +/* +** returns null on error +*/ static t_wordlist *wordlist_expand_wildcards(t_wordlist *inlist) { t_wordlist *outlist; @@ -71,15 +74,20 @@ static t_wordlist *wordlist_expand_wildcards(t_wordlist *inlist) { current = wordlist_pop(&inlist); expansion_result = expand_star(current); - if (expansion_result == NULL) + if (expansion_result == NULL && ft_errno_get() == FT_ESUCCESS) outlist = wordlist_push(outlist, current); - else + else if (expansion_result != NULL) { while (expansion_result != NULL) outlist = wordlist_push(outlist, wordlist_pop(&expansion_result)); worddesc_destroy(current); } + else + { + ft_dprintf(STDERR_FILENO, "minishell: wildcard: expansion fail\n"); + return (wordlist_destroy(outlist), worddesc_destroy(current), NULL); + } } return (outlist); } @@ -87,6 +95,8 @@ static t_wordlist *wordlist_expand_wildcards(t_wordlist *inlist) t_simple_cmd *simple_cmd_expand_wildcards(t_simple_cmd *cmd) { cmd->words = wordlist_expand_wildcards(cmd->words); + if (cmd->words == NULL && ft_errno_get() != FT_ESUCCESS) + return (NULL); ft_errno(FT_ESUCCESS); if (redirect_expand_wildcards(cmd->redirections) == NULL && ft_errno_get() != FT_ESUCCESS) diff --git a/src/subst/wildcard_exp.c b/src/subst/wildcard_exp.c index 98a1ed4..01667f4 100644 --- a/src/subst/wildcard_exp.c +++ b/src/subst/wildcard_exp.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* wildcard_exp.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jguelen +#+ +:+ +#+ */ +/* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/20 15:01:38 by jguelen #+# #+# */ -/* Updated: 2025/04/30 14:07:14 by jguelen ### ########.fr */ +/* Updated: 2025/04/30 14:53:30 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -103,7 +103,7 @@ static t_wordlist *expand_star_core(t_worddesc *file_pattern) current_dir = open_current_dir(); if (current_dir == NULL) - return (NULL); + return (ft_errno(FT_EERRNO), NULL); file_wordlist = NULL; new = readdir(current_dir); while (new) @@ -166,7 +166,7 @@ t_wordlist *expand_star(t_worddesc *file_pattern) t_wordlist *expanded; if (!file_pattern || !file_pattern->word) - return (NULL); + return (ft_errno(FT_EERRNO), NULL); pattern_copy = deal_with_potential_pattern_marker(file_pattern); if (!ispattern(pattern_copy)) return (worddesc_destroy(pattern_copy), NULL);