1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

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.
This commit is contained in:
Martijn Dekker 2020-08-17 20:23:39 +01:00
parent acf84e9633
commit d03e948bcd
5 changed files with 44 additions and 7 deletions

6
NEWS
View file

@ -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

View file

@ -17,4 +17,4 @@
* David Korn <dgk@research.att.com> *
* *
***********************************************************************/
#define SH_RELEASE "93u+m 2020-08-13"
#define SH_RELEASE "93u+m 2020-08-17"

View file

@ -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))

View file

@ -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*)))
{

View file

@ -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))