(fix) Prevented a potential SEGFAULT in expand_star

This commit is contained in:
Jérôme Guélen 2025-04-30 14:10:27 +02:00
parent f8f3d8ccca
commit 3ae29cbdf7
No known key found for this signature in database
3 changed files with 32 additions and 9 deletions

View file

@ -6,7 +6,7 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/22 13:14:27 by kcolin #+# #+# */
/* Updated: 2025/04/25 18:02:13 by jguelen ### ########.fr */
/* Updated: 2025/04/30 14:25:22 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
@ -45,5 +45,8 @@ void build_pattern_checks(char *str, t_worddesc *pattern,
void clean_pattern(t_worddesc *file_pattern);
int ispattern(t_worddesc *desc);
t_worddesc *deal_with_potential_pattern_marker(t_worddesc *file_pattern);
t_wordlist *add_file_to_list(t_wordlist **list, char *filename);
t_wordlist *deal_with_pattern_matching(t_wordlist **file_wordlist,
struct dirent *new, DIR *current_dir, t_worddesc *file_pattern);
#endif

View file

@ -6,11 +6,12 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 15:01:38 by jguelen #+# #+# */
/* Updated: 2025/04/26 15:18:35 by jguelen ### ########.fr */
/* Updated: 2025/04/30 14:07:14 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "../ft_errno.h"
#include "subst.h"
#include "replace_substr.h"
@ -57,7 +58,7 @@ static DIR *open_current_dir(void)
** NOTE: In case of error, this function destroys *list in a similar fashion
** as worddesc_create destroys its first parameter in case of failure.
*/
static t_wordlist *add_file_to_list(t_wordlist **list, char *filename)
t_wordlist *add_file_to_list(t_wordlist **list, char *filename)
{
t_worddesc *file_desc;
char *copy;
@ -107,11 +108,10 @@ static t_wordlist *expand_star_core(t_worddesc *file_pattern)
new = readdir(current_dir);
while (new)
{
if (fits_pattern(new->d_name, file_pattern))
{
if (add_file_to_list(&file_wordlist, new->d_name) == NULL)
return (closedir(current_dir), NULL);
}
file_wordlist = deal_with_pattern_matching(&file_wordlist, new,
current_dir, file_pattern);
if (file_wordlist == NULL && ft_errno_get() != FT_ESUCCESS)
return (NULL);
new = readdir(current_dir);
}
closedir(current_dir);
@ -171,8 +171,11 @@ t_wordlist *expand_star(t_worddesc *file_pattern)
if (!ispattern(pattern_copy))
return (worddesc_destroy(pattern_copy), NULL);
clean_pattern(pattern_copy);
ft_errno(FT_ESUCCESS);
expanded = expand_star_core(pattern_copy);
worddesc_destroy(pattern_copy);
if (!expanded)
return (NULL);
if (ft_strcmp(expanded->word->word, file_pattern->word))
return (expanded);
wordlist_destroy(expanded);

View file

@ -6,10 +6,11 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/15 15:09:56 by jguelen #+# #+# */
/* Updated: 2025/04/26 15:06:41 by jguelen ### ########.fr */
/* Updated: 2025/04/30 14:04:57 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "../ft_errno.h"
#include "subst.h"
#include "replace_substr.h"
@ -101,3 +102,19 @@ t_worddesc *deal_with_potential_pattern_marker(t_worddesc *file_pattern)
}
return (pattern_copy);
}
t_wordlist *deal_with_pattern_matching(t_wordlist **file_wordlist,
struct dirent *new, DIR *current_dir, t_worddesc *file_pattern)
{
char fit;
fit = fits_pattern(new->d_name, file_pattern);
if (fit == 1)
{
if (add_file_to_list(file_wordlist, new->d_name) == NULL)
return (closedir(current_dir), ft_errno(FT_ENOMEM), NULL);
}
else if (fit == -1)
return (closedir(current_dir), ft_errno(FT_ENOMEM), NULL);
return (*file_wordlist);
}