From d1483150ab985ab368f16315a6dbe4f936ab7344 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Tue, 5 Jan 2021 04:55:07 +0000 Subject: [PATCH] 'cd': properly ignore $CDPATH if initial component is '.' or '..' @stephane-chazelas writes: > Per POSIX[*], cd should skip the $CDPATH processing if the first > component of the directory given to cd is . or ... > > Yet, with ksh93u+m 2021-01-03 at least, while that's OK with .., > it's not with . with or without the posix option: > > $ CDPATH=/ ./ksh -o posix -c 'cd -P ./etc && pwd' > /etc > /etc > > It seems to be a regression introduced with ksh93u+ as I can't > reproduce it with ksh93u or any version prior to that. I can also > reproduce in u+, v- and the ksh2020 from the Ubuntu 20.04 > package. src/cmd/ksh93/bltins/cd_pwd.c: b_cd(): - Skip $CDPATH processing not only if the path is absolute, but also if the initial path component is '.' or '..' (in the latter case the $CDPATH processing was done but appeared to be a no-op). src/cmd/ksh93/tests/builtins.sh: - Add regression test. [*] https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/cd.html Fixes: https://github.com/ksh93/ksh/issues/151 --- NEWS | 4 ++++ src/cmd/ksh93/bltins/cd_pwd.c | 10 +++++++--- src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/tests/builtins.sh | 6 ++++++ 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index e7c70848c..3d8720350 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,10 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2021-01-05: + +- Fixed a bug in 'cd' that caused 'cd ./foo' to search for 'foo' in $CDPATH. + 2021-01-03: - The invocation diff --git a/src/cmd/ksh93/bltins/cd_pwd.c b/src/cmd/ksh93/bltins/cd_pwd.c index aae651fce..fcffc398b 100644 --- a/src/cmd/ksh93/bltins/cd_pwd.c +++ b/src/cmd/ksh93/bltins/cd_pwd.c @@ -100,11 +100,15 @@ int b_cd(int argc, char *argv[],Shbltin_t *context) if(shp->subshell && !shp->subshare) sh_subfork(); #endif /* !lib_fchdir */ + /* + * Do $CDPATH processing, except if the path is absolute or the first component is '.' or '..' + */ + if(dir[0] != '/' #if _WINIX - if(*dir != '/' && (dir[1]!=':')) -#else - if(*dir != '/') + && dir[1] != ':' /* on Windows, an initial drive letter plus ':' denotes an absolute path */ #endif /* _WINIX */ + && !(dir[0]=='.' && (dir[1]=='/' || dir[1]==0)) + && !(dir[0]=='.' && dir[1]=='.' && (dir[2]=='/' || dir[2]==0))) { if(!(cdpath = (Pathcomp_t*)shp->cdpathlist) && (dp=sh_scoped(shp,CDPNOD)->nvalue.cp)) { diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index c6db0fbef..c8c295671 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -20,7 +20,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.0.0-alpha" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2021-01-03" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2021-01-05" /* must be in this format for $((.sh.version)) */ /* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */ /* Arithmetic $((.sh.version)) uses the last 10 chars, so the date must be at the end. */ diff --git a/src/cmd/ksh93/tests/builtins.sh b/src/cmd/ksh93/tests/builtins.sh index a482789b6..88f3c6e01 100755 --- a/src/cmd/ksh93/tests/builtins.sh +++ b/src/cmd/ksh93/tests/builtins.sh @@ -832,6 +832,12 @@ actual=$({ a=$(cd; pwd); } >&-; print -r -- "$a") [[ $actual == "$expect" ]] || err_exit "'cd' broke new-form command substitution with outer stdout closed" \ "(expected $(printf %q "$expect"), got $(printf %q "$actual"))" +# CDPATH was not ignored by 'cd ./dir': https://github.com/ksh93/ksh/issues/151 +expect=': cd: ./dev: [No such file or directory]' +actual=$( (CDPATH=/ cd -P ./dev && pwd) 2>&1 ) +let "(e=$?)==1" && [[ $actual == *"$expect" ]] || err_exit "CDPATH not ignored by cd ./dir" \ + "(expected *$(printf %q "$expect") with status 1, got $(printf %q "$actual") with status $e)" + # ====== # 'readonly' should set the correct scope when creating variables in functions unset foo