From d03e948bcdaa105086f7fb02a69df5f05eb9f03f Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Mon, 17 Aug 2020 20:23:39 +0100 Subject: [PATCH] Fix 'command -p' lookup if hash table entry exists (re: c9ccee86) If a command's path was previously added to the hash table as a 'tracked alias', then the hash table entry was used, bypassing the default utility path search activated by 'command -p'. 'command -p' activates a SH_DEFPATH shell state. The bug was caused by a failure to check for this state before using the hash table. This check needs to be added in four places. src/cmd/ksh93/sh/path.c, src/cmd/ksh93/sh/xec.c: - path_search(), path_spawn(), sh_exec(), sh_ntfork(): Only consult the hash table, which is shp->track_tree, if the SH_DEFPATH shell state is not active. src/cmd/ksh93/tests/path.sh: - Add regress tests checking that 'command -p' and 'command -p -v' still search in the default path if a hash table entry exists for the command searched. --- NEWS | 6 ++++++ src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/sh/path.c | 10 ++++++++-- src/cmd/ksh93/sh/xec.c | 12 ++++++++++-- src/cmd/ksh93/tests/path.sh | 21 +++++++++++++++++++-- 5 files changed, 44 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 888814b9f..b6fdf57da 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,12 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2020-08-17: + +- 'command -p' incorrectly used the hash table entry (a.k.a. tracked alias) + for a command if its path was previously hashed. It has now been fixed so + it never consults the hash table. + 2020-08-16: - Fixed 'command -x' on macOS, Linux and Solaris by accounting for a 16-byte diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index ef37df3c3..8899cd5ca 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -17,4 +17,4 @@ * David Korn * * * ***********************************************************************/ -#define SH_RELEASE "93u+m 2020-08-13" +#define SH_RELEASE "93u+m 2020-08-17" diff --git a/src/cmd/ksh93/sh/path.c b/src/cmd/ksh93/sh/path.c index 9b683539f..62395ab8a 100644 --- a/src/cmd/ksh93/sh/path.c +++ b/src/cmd/ksh93/sh/path.c @@ -696,7 +696,12 @@ int path_search(Shell_t *shp,register const char *name,Pathcomp_t **oldpp, int f path_init(shp); if(flag) { - if(!(flag&1) && (np=nv_search(name,shp->track_tree,0)) && !nv_isattr(np,NV_NOALIAS) && (pp=(Pathcomp_t*)np->nvalue.cp)) + /* if a tracked alias exists and we're not searching the default path, use it */ + if(!sh_isstate(SH_DEFPATH) + && !(flag&1) + && (np=nv_search(name,shp->track_tree,0)) + && !nv_isattr(np,NV_NOALIAS) + && (pp=(Pathcomp_t*)np->nvalue.cp)) { stakseek(PATH_OFFSET); path_nextcomp(shp,pp,name,pp); @@ -1070,7 +1075,8 @@ pid_t path_spawn(Shell_t *shp,const char *opath,register char **argv, char **env pidsize = sfprintf(stkstd,"*%d*",spawn?getpid():getppid()); stakputs(opath); opath = stakfreeze(1)+PATH_OFFSET+pidsize; - np=nv_search(argv[0],shp->track_tree,0); + /* only use tracked alias if we're not searching default path */ + np = sh_isstate(SH_DEFPATH) ? NIL(Namval_t*) : nv_search(argv[0],shp->track_tree,0); while(libpath && !libpath->lib) libpath=libpath->next; if(libpath && (!np || nv_size(np)>0)) diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c index c79dc6b7b..bddd48a4d 100644 --- a/src/cmd/ksh93/sh/xec.c +++ b/src/cmd/ksh93/sh/xec.c @@ -1221,7 +1221,11 @@ int sh_exec(register const Shnode_t *t, int flags) } else { - if((np=nv_search(com0,shp->track_tree,0)) && !nv_isattr(np,NV_NOALIAS) && np->nvalue.cp) + /* if a tracked alias exists and we're not searching the default path, use it */ + if(!sh_isstate(SH_DEFPATH) + && (np=nv_search(com0,shp->track_tree,0)) + && !nv_isattr(np,NV_NOALIAS) + && np->nvalue.cp) np=nv_search(nv_getval(np),shp->bltin_tree,0); else np = 0; @@ -3639,7 +3643,11 @@ static pid_t sh_ntfork(Shell_t *shp,const Shnode_t *t,char *argv[],int *jobid,in if(!strchr(path=argv[0],'/')) { Namval_t *np; - if((np=nv_search(path,shp->track_tree,0)) && !nv_isattr(np,NV_NOALIAS) && np->nvalue.cp) + /* if a tracked alias exists and we're not searching the default path, use it */ + if(!sh_isstate(SH_DEFPATH) + && (np=nv_search(path,shp->track_tree,0)) + && !nv_isattr(np,NV_NOALIAS) + && np->nvalue.cp) path = nv_getval(np); else if(path_absolute(shp,path,NIL(Pathcomp_t*))) { diff --git a/src/cmd/ksh93/tests/path.sh b/src/cmd/ksh93/tests/path.sh index c02484d9d..a2b0c6920 100755 --- a/src/cmd/ksh93/tests/path.sh +++ b/src/cmd/ksh93/tests/path.sh @@ -404,8 +404,10 @@ END # ====== # Check that 'command -p' searches the default OS utility PATH. -v=$(PATH=/dev/null "$SHELL" -c 'command -p ls /dev/null') -[[ $v == /dev/null ]] || err_exit 'command -p fails to find standard utility' +expect=/dev/null +actual=$(PATH=/dev/null "$SHELL" -c 'command -p ls /dev/null' 2>&1) +[[ $actual == "$expect" ]] || err_exit 'command -p fails to find standard utility' \ + "(expected $(printf %q "$expect"), got $(printf %q "$actual"))" # ksh segfaults if $PATH contains a .paths directory mkdir -p $tmp/paths-dir-crash/ @@ -416,6 +418,21 @@ print ok EOF [[ $($SHELL $tmp/paths-dir-crash/run.sh 2>/dev/null) == ok ]] || err_exit "ksh crashes if PATH contains a .paths directory" +# Check that 'command -p' and 'command -p -v' do not use the hash table (a.k.a. tracked aliases). +print 'echo "wrong path used"' > $tmp/ls +chmod +x $tmp/ls +expect=/dev/null +actual=$(PATH=$tmp; redirect 2>&1; hash ls; command -p ls /dev/null) +[[ $actual == "$expect" ]] || err_exit "'command -p' fails to search default path if tracked alias exists" \ + "(expected $(printf %q "$expect"), got $(printf %q "$actual"))" +actual=$(PATH=$tmp; redirect 2>&1; hash ls; command -p ls /dev/null; exit) # the 'exit' disables subshell optimization +[[ $actual == "$expect" ]] || err_exit "'command -p' fails to search default path if tracked alias exists" \ + "(expected $(printf %q "$expect"), got $(printf %q "$actual"))" +expect=$(builtin getconf; PATH=$(getconf PATH); whence -p ls) +actual=$(PATH=$tmp; redirect 2>&1; hash ls; command -p -v ls) +[[ $actual == "$expect" ]] || err_exit "'command -p -v' fails to search default path if tracked alias exists" \ + "(expected $(printf %q "$expect"), got $(printf %q "$actual"))" + # ====== exit $((Errors<125?Errors:125))