Browse Source

[enh] fix mleaks on replacement f().
- ft_strdup: if NULL, return NULL.
- add unit tests for env var.

Moul 2 years ago
parent
commit
eba0cd03cf
3 changed files with 16 additions and 6 deletions
  1. 2 0
      libft/src/ft_strdup.c
  2. 11 5
      src/exec.c
  3. 3 1
      tests.sh

+ 2 - 0
libft/src/ft_strdup.c

@@ -17,6 +17,8 @@ char	*ft_strdup(const char *s1)
 	char	*s;
 	char	*sc;
 
+	if (s1 == NULL)
+		return (NULL);
 	s = (char *)s1;
 	sc = ft_strnew(ft_strlen(s));
 	ft_strcpy(sc, (char *)s1);

+ 11 - 5
src/exec.c

@@ -107,16 +107,22 @@ void	exec_if_perm_ok(t_e *e, char *path, char **cmd)
 char	**change_tilde_to_home(t_e *e, char **cmd)
 {
 	int		i;
+	char	*tmp;
 
 	i = -1;
 	while (++i < ft_tabsize(cmd))
 	{
 		if (cmd[i] && ft_strcmp(cmd[i], "~") == 0 && e->home != NULL)
-			cmd[i] = ft_strdup(e->home);
-		if (cmd[i] && cmd[i][0] == '~' && cmd[i][1] == '/' && e->home != NULL)
-			cmd[i] = ft_strjoin(e->home, &cmd[i][1]);
-		if (cmd[i][0] == '$' && cmd[i][1] != '\0')
-			cmd[i] = get_tenv(e, &cmd[i][1]);
+			tmp = ft_strdup(e->home);
+		else if (cmd[i] && cmd[i][0] == '~' && cmd[i][1] == '/' && \
+				e->home != NULL)
+			tmp = ft_strjoin(e->home, &cmd[i][1]);
+		else if (cmd[i][0] == '$' && cmd[i][1] != '\0')
+			tmp = ft_strdup(get_tenv(e, &cmd[i][1]));
+		else
+			tmp = ft_strdup(cmd[i]);
+		ft_strdel(&cmd[i]);
+		cmd[i] = tmp;
 	}
 	return (cmd);
 }

+ 3 - 1
tests.sh

@@ -35,7 +35,9 @@ tests=(
 ## ENV ##
 "env"
 "export TOTO=toto" # test setenv
-#"unsetenv TOTO"
+"echo $PATH"
+"unsetenv PATH; echo $PATH"
+"unsetenv TOTO"
 
 ## Other ##
 "pwd"