Skip to content

Commit eee47df

Browse files
committed
Fix handling of skipped directories when autoloading functions
Fix a bug in autoloading functions. Directories in the path search list which should be skipped (e.g. because they don't exist) did not interact correctly with autoloaded functions, so that a function to autoload was not always found. Details: att#1454 Fix backported (and cleaned up) from: att@3bc58164 src/cmd/ksh93/sh/path.c: - path_opentype(): Fix the path search loop so that entries marked with PATH_SKIP are handled correctly. src/cmd/ksh93/tests/functions.sh: - Add regression test verifying an autoloaded function with a PATH that triggered the bug. The bug in path_opentype() fixed by this commit may affect other scenarios but we know it affects autoloaded functions. Hence the test for that scenario. (cherry picked from commit a27903165775309f4f032de5d42ec1785f14cfbc)
1 parent 482d1c3 commit eee47df

4 files changed

Lines changed: 49 additions & 7 deletions

File tree

‎NEWS‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ For full details, see the git log at:
44

55
Any uppercase BUG_* names are modernish shell bug IDs.
66

7+
2020-05-31:
8+
9+
- Fix a bug in autoloading functions. Directories in the path search list
10+
which should be skipped (e.g. because they don't exist) did not interact
11+
correctly with autoloaded functions, so that a function to autoload was
12+
not always found correctly.
13+
Details: https://github.com/att/ast/issues/1454
14+
715
2020-05-30:
816

917
- Fix POSIX compliance of 'test'/'[' exit status on error. The command now

‎src/cmd/ksh93/include/version.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
* David Korn *
1818
* *
1919
***********************************************************************/
20-
#define SH_RELEASE "93u+m 2020-05-30"
20+
#define SH_RELEASE "93u+m 2020-05-31"

‎src/cmd/ksh93/sh/path.c‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -508,20 +508,24 @@ static int path_opentype(Shell_t *shp,const char *name, register Pathcomp_t *pp,
508508
{
509509
register int fd= -1;
510510
struct stat statb;
511-
Pathcomp_t *oldpp;
511+
Pathcomp_t *nextpp;
512+
512513
if(!pp && !shp->pathlist)
513514
path_init(shp);
514515
if(!fun && strchr(name,'/'))
515516
{
516517
if(sh_isoption(SH_RESTRICTED))
517518
errormsg(SH_DICT,ERROR_exit(1),e_restricted,name);
518519
}
520+
521+
nextpp = pp;
519522
do
520523
{
521-
pp = path_nextcomp(shp,oldpp=pp,name,0);
522-
while(oldpp && (oldpp->flags&PATH_SKIP))
523-
oldpp = oldpp->next;
524-
if(fun && (!oldpp || !(oldpp->flags&PATH_FPATH)))
524+
pp = nextpp;
525+
nextpp = path_nextcomp(shp,pp,name,0);
526+
if(pp && (pp->flags&PATH_SKIP))
527+
continue;
528+
if(fun && (!pp || !(pp->flags&PATH_FPATH)))
525529
continue;
526530
if((fd = sh_open(path_relative(shp,stakptr(PATH_OFFSET)),O_RDONLY,0)) >= 0)
527531
{
@@ -533,7 +537,8 @@ static int path_opentype(Shell_t *shp,const char *name, register Pathcomp_t *pp,
533537
}
534538
}
535539
}
536-
while( fd<0 && pp);
540+
while(fd<0 && nextpp);
541+
537542
if(fd>=0 && (fd = sh_iomovefd(fd)) > 0)
538543
{
539544
fcntl(fd,F_SETFD,FD_CLOEXEC);

‎src/cmd/ksh93/tests/functions.sh‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,4 +1213,33 @@ rc=$?
12131213
exp=$((256+$(kill -l TERM) ))
12141214
[[ $rc == "$exp" ]] || err_exit "expected exitval $exp got $rc"
12151215
1216+
# ======
1217+
# Verify that directories in the path search list which should be skipped
1218+
# (e.g. because they don't exist) interact correctly with autoloaded functions.
1219+
# See https://github.com/att/ast/issues/1454
1220+
expect="Func cd called with |$tmp/usr|
1221+
$tmp/usr"
1222+
actual=$(
1223+
set -- wrong args passed
1224+
mkdir -p "$tmp/usr/bin"
1225+
print 'echo "wrong file executed ($*)"' >"$tmp/usr/bin/cd"
1226+
prefix=$tmp/ksh.$$
1227+
1228+
FPATH=$prefix/bad:$prefix/functions
1229+
mkdir -p "$prefix/functions"
1230+
print 'function cd { echo "Func cd called with |$*|"; command cd "$@"; }' >"$prefix/functions/cd"
1231+
typeset -fu cd
1232+
1233+
PATH=$tmp/arglebargle:$PATH:$tmp/usr/bin:$tmp/bin
1234+
cd "$tmp/usr"
1235+
pwd
1236+
)
1237+
actual_status=$?
1238+
expect_status=0
1239+
[[ $actual_status == "$expect_status" ]] ||
1240+
err_exit "autoload function skipped dir test wrong status (expected $expect_status, got $actual_status)"
1241+
[[ $actual == "$expect" ]] ||
1242+
err_exit "autoload function skipped dir test wrong output (expected $(printf %q "$expect"), got $(printf %q "$actual"))"
1243+
1244+
# ======
12161245
exit $((Errors<125?Errors:125))

0 commit comments

Comments
 (0)