diff --git a/NEWS b/NEWS index e26687e14..ae1c17648 100644 --- a/NEWS +++ b/NEWS @@ -9,6 +9,9 @@ Any uppercase BUG_* names are modernish shell bug IDs. shell when suspending (Ctrl+Z) an external command invoked by a dot script or POSIX shell function, or via 'eval'. +- Fixed a memory leak that occurred when running a command that was found on + the PATH. + 2022-01-26: - On Cygwin, ksh now executes scripts that do not have a #! path itself, diff --git a/src/cmd/ksh93/sh/path.c b/src/cmd/ksh93/sh/path.c index eef684508..9df05bf13 100644 --- a/src/cmd/ksh93/sh/path.c +++ b/src/cmd/ksh93/sh/path.c @@ -58,14 +58,21 @@ static int checkdotpaths(Pathcomp_t*,Pathcomp_t*,Pathcomp_t*,int); static void checkdup(register Pathcomp_t*); static Pathcomp_t *defpathinit(void); -static const char *defpath; /* default path that finds standard utilities */ +static const char *std_path(void) +{ + static const char *defpath; /* default path that finds standard utilities */ + if(!defpath) + { + if(!(defpath = astconf("PATH",NIL(char*),NIL(char*)))) + abort(); + defpath = sh_strdup(defpath); /* the value returned by astconf() is short-lived */ + } + return(defpath); +} static int ondefpath(const char *name) { - const char *cp; - if(!defpath) - defpathinit(); - cp = defpath; + const char *cp = std_path(); if(cp) { const char *sp; @@ -433,13 +440,7 @@ Pathcomp_t *path_nextcomp(register Pathcomp_t *pp, const char *name, Pathcomp_t static Pathcomp_t* defpathinit(void) { - if(!defpath) - { - if(!(defpath = astconf("PATH",NIL(char*),NIL(char*)))) - abort(); - defpath = sh_strdup(defpath); /* the value returned by astconf() is short-lived */ - } - return(path_addpath((Pathcomp_t*)0,(defpath),PATH_PATH)); + return(path_addpath((Pathcomp_t*)0,std_path(),PATH_PATH)); } static void pathinit(void)