fix: fixed leak on variable expansion
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/10/22 18:37:38 by sede-san #+# #+# */
|
/* Created: 2025/10/22 18:37:38 by sede-san #+# #+# */
|
||||||
/* Updated: 2026/02/13 09:22:58 by sede-san ### ########.fr */
|
/* Updated: 2026/02/13 13:30:21 by sede-san ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -109,6 +109,22 @@ static t_list *parse_tokens(
|
|||||||
return (commands);
|
return (commands);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static char *replace_value(
|
||||||
|
char *original,
|
||||||
|
char *value,
|
||||||
|
int start,
|
||||||
|
int end
|
||||||
|
)
|
||||||
|
{
|
||||||
|
const char *before = ft_substr(original, 0, start);
|
||||||
|
const char *after = ft_substr(original, end, ft_strlen(original) - end);
|
||||||
|
const char *expanded = ft_strnjoin(3, before, value, after);
|
||||||
|
|
||||||
|
free((char *)before);
|
||||||
|
free((char *)after);
|
||||||
|
return ((char *)expanded);
|
||||||
|
}
|
||||||
|
|
||||||
static void expand_variable(
|
static void expand_variable(
|
||||||
char **argument,
|
char **argument,
|
||||||
int *i,
|
int *i,
|
||||||
@@ -131,10 +147,7 @@ static void expand_variable(
|
|||||||
free(variable_name);
|
free(variable_name);
|
||||||
if (variable == NULL)
|
if (variable == NULL)
|
||||||
variable = "";
|
variable = "";
|
||||||
expanded = ft_strnjoin(3,
|
expanded = replace_value(*argument, variable, start - 1, end);
|
||||||
ft_substr(*argument, 0, *i),
|
|
||||||
variable,
|
|
||||||
ft_substr(*argument,end, ft_strlen(*argument) - end));
|
|
||||||
if (expanded == NULL)
|
if (expanded == NULL)
|
||||||
return (minishell->exit = true, malloc_error());
|
return (minishell->exit = true, malloc_error());
|
||||||
*i += ft_strlen(variable) - 1;
|
*i += ft_strlen(variable) - 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user