From e8deb0be1950386b0804123e758a6b919c4b1f18 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Tue, 21 Feb 2023 14:34:49 +0100 Subject: [PATCH] fix: leak '0' creation --- env2.c | 13 +++++++++---- env3.c | 26 +++++++++++++++++++++++++- main.c | 10 ++++++++-- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/env2.c b/env2.c index 0a93049..76f0008 100644 --- a/env2.c +++ b/env2.c @@ -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,10 +46,15 @@ void env_del(void *ptr) { t_env *content; + if (ptr == NULL) + return ; content = ptr; - free(content->key); - free(content->value); - free(content); + if (content->key != NULL) + free(content->key); + if (content->key != NULL) + free(content->value); + if (content != NULL) + free(content); } char **env_to_strs(t_list **head) diff --git a/env3.c b/env3.c index 3be208a..3c69c62 100644 --- a/env3.c +++ b/env3.c @@ -6,10 +6,11 @@ /* 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" 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); +} diff --git a/main.c b/main.c index 04dd84e..b67da56 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: cchauvet