mirror of
git://git.code.sf.net/p/cdesktopenv/code
synced 2025-03-09 15:50:02 +00:00
cd - shouldn't ignore $OLDPWD when in a new scope (#249)
This bug was first reported at <https://github.com/att/ast/issues/8>. The 'cd' command currently takes the value of $OLDPWD from the wrong scope. In the following example 'cd -' will change the directory to /bin instead of /tmp: $ OLDPWD=/bin ksh93 -c 'OLDPWD=/tmp cd -' /bin src/cmd/ksh93/bltins/cd_pwd.c: - Use sh_scoped() to obtain the correct value of $OLDPWD. - Fix a use-after-free bug. Make the 'oldpwd' variable a static char that points to freeable memory. Each time cd is used, this variable is freed if it points to a freeable memory address and isn't also a pointer to shp->pwd. src/cmd/ksh93/sh/path.c: path_pwd(): - Simplify and add comments. - Scope $PWD properly. src/cmd/ksh93/tests/builtins.sh, src/cmd/ksh93/tests/leaks.sh: - Backport the ksh2020 regression tests for 'cd -' when $OLDPWD is set. - Add test for $OLDPWD and $PWD after subshare. - Add test for $PWD after 'cd'. - Add test for possible memory leak. - Add testing for 'unset' on OLDPWD and PWD. src/cmd/ksh93/COMPATIBILITY: - Add compatibility note about changes to $PWD and $OLDPWD. Co-authored-by: Martijn Dekker <martijn@inlv.org>
This commit is contained in:
parent
ed478ab7e3
commit
ca2443b58c
7 changed files with 148 additions and 41 deletions
|
|
@ -244,49 +244,35 @@ static pid_t path_xargs(Shell_t *shp,const char *path, char *argv[],char *const
|
|||
char *path_pwd(Shell_t *shp,int flag)
|
||||
{
|
||||
register char *cp;
|
||||
register int count = 0;
|
||||
Namval_t *pwdnod;
|
||||
NOT_USED(flag);
|
||||
/* Don't bother if PWD already set */
|
||||
if(shp->pwd)
|
||||
return((char*)shp->pwd);
|
||||
while(1)
|
||||
/* First see if PWD variable is correct */
|
||||
pwdnod = sh_scoped(shp,PWDNOD);
|
||||
cp = nv_getval(pwdnod);
|
||||
if(!(cp && *cp=='/' && test_inode(cp,e_dot)))
|
||||
{
|
||||
/* try from lowest to highest */
|
||||
switch(count++)
|
||||
/* Check if $HOME is a path to the PWD; this ensures $PWD == $HOME
|
||||
at login, even if $HOME is a path that contains symlinks */
|
||||
cp = nv_getval(sh_scoped(shp,HOME));
|
||||
if(!(cp && *cp=='/' && test_inode(cp,e_dot)))
|
||||
{
|
||||
case 0:
|
||||
cp = nv_getval(PWDNOD);
|
||||
break;
|
||||
case 1:
|
||||
cp = nv_getval(HOME);
|
||||
break;
|
||||
case 2:
|
||||
cp = "/";
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
if(cp=getcwd(NIL(char*),0))
|
||||
{
|
||||
nv_offattr(PWDNOD,NV_NOFREE);
|
||||
_nv_unset(PWDNOD,0);
|
||||
PWDNOD->nvalue.cp = cp;
|
||||
goto skip;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 4:
|
||||
/* Get physical PWD (no symlinks) using getcwd(3), fall back to "." */
|
||||
cp = getcwd(NIL(char*),0);
|
||||
if(!cp)
|
||||
return((char*)e_dot);
|
||||
}
|
||||
if(cp && *cp=='/' && test_inode(cp,e_dot))
|
||||
break;
|
||||
/* Store in PWD variable */
|
||||
if(shp->subshell)
|
||||
pwdnod = sh_assignok(pwdnod,1);
|
||||
nv_offattr(pwdnod,NV_NOFREE);
|
||||
nv_putval(pwdnod,cp,NV_RDONLY);
|
||||
}
|
||||
if(count>1)
|
||||
{
|
||||
nv_offattr(PWDNOD,NV_NOFREE);
|
||||
nv_putval(PWDNOD,cp,NV_RDONLY);
|
||||
}
|
||||
skip:
|
||||
nv_onattr(PWDNOD,NV_NOFREE|NV_EXPORT);
|
||||
shp->pwd = (char*)(PWDNOD->nvalue.cp);
|
||||
nv_onattr(pwdnod,NV_NOFREE|NV_EXPORT);
|
||||
/* Set shell PWD */
|
||||
shp->pwd = (char*)(pwdnod->nvalue.cp);
|
||||
return(cp);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue