fix: leak '0' creation

This commit is contained in:
Camille Chauvet 2023-02-21 14:34:49 +01:00
parent ca09c8ed26
commit e8deb0be19
3 changed files with 42 additions and 7 deletions

7
env2.c
View File

@ -1,4 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env2.c :+: :+: :+: */
@ -10,6 +9,7 @@
/* */
/* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h"
int get_index(char *s, char c)
@ -46,9 +46,14 @@ void env_del(void *ptr)
{
t_env *content;
if (ptr == NULL)
return ;
content = ptr;
if (content->key != NULL)
free(content->key);
if (content->key != NULL)
free(content->value);
if (content != NULL)
free(content);
}

26
env3.c
View File

@ -6,10 +6,11 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 17:25:09 by erey-bet #+# #+# */
/* Updated: 2023/02/17 17:31:31 by erey-bet ### ########.fr */
/* Updated: 2023/02/21 14:09:42 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h"
char *get_value(char *str)
@ -37,3 +38,26 @@ char *get_key(char *str)
s[i] = str[i];
return (s);
}
int create_value_by_key_dup(char *key, char *value, t_list **env)
{
char *key_dup;
char *value_dup;
key_dup = ft_strdup(key);
if (key_dup == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
value_dup = ft_strdup(value);
if (value_dup == NULL)
{
free(key);
ft_eprintf("minishell: malloc failed\n");
return (1);
}
if (create_value_by_key(key_dup, value_dup, env))
return (1);
return (0);
}

10
main.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
/* Updated: 2023/02/20 15:24:42 by starnakin ### ########.fr */
/* Updated: 2023/02/21 14:24:45 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -108,7 +108,13 @@ int main(int ac, char **av, char **env)
data.env = init_env(env);
if (data.env == NULL)
return (1);
create_value_by_key("", "$", data.env);
if (create_value_by_key_dup("", "$", data.env) == 1
|| create_value_by_key_dup("?", "0", data.env) == 1)
{
ft_lstclear(data.env, env_del);
free(data.env);
return (1);
}
line = ft_get_user_input(data.env);
if (line == NULL)
{