From 7d0d45b09ad2beb677ed1a22d37db1e40786519e Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sat, 12 Dec 2020 16:50:46 +0100 Subject: Securing mallocs --- src/f_alloc.c | 12 ++++++++++-- src/f_alloc.h | 1 + src/m_minishell.c | 2 +- src/s_init.c | 12 +++++++++--- src/s_init_next.c | 9 +++++++-- src/s_init_next.h | 2 +- src/u_vars_next.c | 24 ++++++++++++++---------- src/u_vars_next.h | 2 +- 8 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/f_alloc.c b/src/f_alloc.c index dc3f589..5f42f09 100644 --- a/src/f_alloc.c +++ b/src/f_alloc.c @@ -10,16 +10,24 @@ /* */ /* ************************************************************************** */ +#include #include #include -#include #include -#include +#include #include "s_destroy.h" #include "s_line.h" #include "s_struct.h" +void f_alloc(t_msh *msh) +{ + char tmp[255]; + + ft_strlcpy(tmp, msh->argv[0], 255); + ft_dprintf(STDERR_FILENO, "%s: %s\n", tmp, strerror(errno)); +} + void f_alloc_and_destroy_msh(t_msh *msh) { char tmp[255]; diff --git a/src/f_alloc.h b/src/f_alloc.h index a520eeb..6a72088 100644 --- a/src/f_alloc.h +++ b/src/f_alloc.h @@ -15,6 +15,7 @@ # include "s_struct.h" +void f_alloc(t_msh *msh); void f_alloc_and_destroy_msh(t_msh *msh); void f_alloc_and_clear_line(t_msh *msh); diff --git a/src/m_minishell.c b/src/m_minishell.c index 987cfc0..afec0de 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -29,7 +29,7 @@ int main(int argc, char *const argv[], char *const envp[]) if ((msh = s_init_msh(argc, argv, envp)) == NULL) { - ft_dprintf(STDERR_FILENO, "%s\n", strerror(errno)); + ft_dprintf(STDERR_FILENO, "minishell: %s\n", strerror(errno)); return (M_RET_ALLOC); } ret = m_argv(argc, argv, msh); diff --git a/src/s_init.c b/src/s_init.c index a572c32..a13f9f1 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -11,13 +11,13 @@ /* ************************************************************************** */ #include -#include -#include #ifdef __linux__ # include #else # include #endif +#include +#include #include "b_export_next.h" #include "d_define.h" @@ -108,8 +108,12 @@ t_msh *s_init_msh(int argc, char *const argv[], char *const envp[]) if ((msh = (t_msh*)malloc(sizeof(t_msh))) == NULL) return (NULL); msh->envp = NULL; + (void)envp; if ((msh->envp = s_dupenv(envp)) == NULL) + { + ft_memdel((void*)&msh); return (NULL); + } msh->argc = argc - 1; msh->argv = (char**)argv; msh->ret = 0; @@ -118,11 +122,13 @@ t_msh *s_init_msh(int argc, char *const argv[], char *const envp[]) s_set_cwd(cwd, msh); if ((msh->cwd = ft_strdup(cwd)) == NULL) { + ft_delwords(msh->envp); ft_memdel((void*)&msh); return (NULL); } msh->env_fork_tmp[0][0] = '\0'; - s_inc_shlvl(msh); + if (s_inc_shlvl(msh) == 1) + return (NULL); s_init_sqb_ref_one(msh); s_init_sqb_ref_two(msh); s_init_sqb_ref_thr(msh); diff --git a/src/s_init_next.c b/src/s_init_next.c index d506d9d..3cc676d 100644 --- a/src/s_init_next.c +++ b/src/s_init_next.c @@ -24,11 +24,12 @@ #include "b_export_next.h" #include "f_fail.h" +#include "s_destroy.h" #include "s_struct.h" #include "u_vars.h" #include "u_vars_next.h" -void s_inc_shlvl(t_msh *msh) +char s_inc_shlvl(t_msh *msh) { char fmt[8]; char tmp[255]; @@ -49,8 +50,12 @@ void s_inc_shlvl(t_msh *msh) shlvl = (shlvl >= 999) ? 0 : shlvl; shlvl = (shlvl < 0) ? 0 : shlvl + 1; ft_itoa_s(str, shlvl); - u_subst_var_value("$SHLVL", str, msh); + if (u_subst_var_value("$SHLVL", str, msh) == 1) + { + return (1); + } } + return (0); } char **s_dupenv_del(char **nenvp, unsigned long i) diff --git a/src/s_init_next.h b/src/s_init_next.h index dbb6cd4..650e795 100644 --- a/src/s_init_next.h +++ b/src/s_init_next.h @@ -15,7 +15,7 @@ # include "s_struct.h" -void s_inc_shlvl(t_msh *msh); +char s_inc_shlvl(t_msh *msh); char **s_dupenv_del(char **nenvp, unsigned long i); char **s_dupenv(char *const envp[]); void s_set_cwd(char cwd[], t_msh *msh); diff --git a/src/u_vars_next.c b/src/u_vars_next.c index 22288d2..5ffdbb1 100644 --- a/src/u_vars_next.c +++ b/src/u_vars_next.c @@ -65,34 +65,37 @@ static t_bool u_get_custom_var_existance(const char varname[], t_msh *msh) } /* -** void +** SYNOPSIS +** char ** u_subst_var_value(const char varname[], const char newval[], t_msh *msh); ** ** DESCRIPTION -** The subst_var_value() changes the value of msh->envp +** The u_subst_var_value() function changes the value of msh->envp ** variable varname[] with newval[]. If varname[] wasn't found ** in msh->envp, varname[] is searched in msh->vars. Otherwise ** varname[] is added to msh->vars. +** +** RETURN VALUES +** The u_subst_var_value() function returns 0 upon success +** and 1 in case of malloc(3) failure. */ -/* -** TODO: Handle malloc fail -*/ - -void u_subst_var_value(const char varname[], +char u_subst_var_value(const char varname[], const char newval[], t_msh *msh) { - char new_line_fmt[ARG_MAX]; long env_line; + char new_line_fmt[ARG_MAX]; if ((env_line = u_get_env_var_line(varname + 1, msh)) > -1) { ft_memdel((void*)&msh->envp[env_line]); ft_sprintf(new_line_fmt, "%s=%s", varname + 1, newval); - if ((msh->envp[env_line] = (char*)malloc((ft_strlen(new_line_fmt) + 1) * - sizeof(char))) == NULL) + /* if ((msh->envp[env_line] = (char*)malloc((ft_strlen(new_line_fmt) + 1) * */ + /* sizeof(char))) == NULL) */ + if ((msh->envp[env_line] = NULL) == NULL) { + return (1); } (void)ft_strlcpy(msh->envp[env_line], new_line_fmt, ft_strlen(new_line_fmt) + 1); @@ -105,4 +108,5 @@ void u_subst_var_value(const char varname[], { lvars_add_front(&msh->vars, lvars_new(varname + 1, newval)); } + return (0); } diff --git a/src/u_vars_next.h b/src/u_vars_next.h index cd802c7..2defb7b 100644 --- a/src/u_vars_next.h +++ b/src/u_vars_next.h @@ -15,7 +15,7 @@ # include "s_struct.h" -void u_subst_var_value(const char varname[], +char u_subst_var_value(const char varname[], const char newval[], t_msh *msh); -- cgit v1.2.3