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

13
env2.c
View File

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

26
env3.c
View File

@ -6,10 +6,11 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 17:25:09 by erey-bet #+# #+# */ /* 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" #include "minishell.h"
char *get_value(char *str) char *get_value(char *str)
@ -37,3 +38,26 @@ char *get_key(char *str)
s[i] = str[i]; s[i] = str[i];
return (s); 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 +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */ /* 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); data.env = init_env(env);
if (data.env == NULL) if (data.env == NULL)
return (1); 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); line = ft_get_user_input(data.env);
if (line == NULL) if (line == NULL)
{ {