mirror of
git://git.code.sf.net/p/cdesktopenv/code
synced 2025-03-09 15:50:02 +00:00
This commit fixes what are hopefully the two final aspects of #153: 1. If the present working directory does not exist (was moved or deleted) upon entering a virtual subshell, no PWD directory path is saved. Since restoring the state after exiting a virtual subshell is contingent on a previous PWD path existing, this resulted in entire aspects of the virtual subshell, such as the subshell function tree, not being cleaned up. 2. A separate problem is that 'cd ..' does not update PWD or OLDPWD when run from a nonexistent directory. A reproducer exposing both problems is: $ mkdir test $ cd test $ ksh -c '(subfn() { BAD; }; cd ..; echo subPWD==$PWD); typeset -f subfn; echo mainPWD==$PWD' subPWD==/usr/local/src/ksh93/ksh/test subfn() { BAD; };mainPWD==/usr/local/src/ksh93/ksh/test Expected output: subPWD==/usr/local/src/ksh93/ksh mainPWD==/usr/local/src/ksh93/ksh/test src/cmd/ksh93/bltins/cd_pwd.c: - If path_pwd() fails to get the PWD (usually it no longer exists), don't set $OLDPWD to '.' as that is pointless; use $PWD instead. After cd'ing from a nonexistent directory, 'cd -' *should* fail and should not be equivalent to 'cd .'. - Remove a redundant check for (!oldpwd) where it is always set. - Do not prematurely return without setting PWD or OLDPWD if pathcanon() fails to canonicalise a nonexistent directory. Instead, fall back to setting PWD to the result of getcwd(3). src/cmd/ksh93/sh/subshell.c: - Minor stylistic adjustment. Some NULL macros sneaked in. This historic code base does not use them (yet); change to NIL(type*). - sh_subshell(): Fix logic for determining whether to save/restore subshell state. 1. When saving, 'if(!comsub || !shp->subshare)' is redundant; 'if(!shp->subshare)' should be enough. If we're not in a subshare, state should be saved. 2. When restoring, 'if(sp->shpwd)' is just nonsense as there is no guarantee that the PWD exists upon entering a subshell. Simply use the same 'if(!shp->subshare)'. Add an extra check for sp->pwd to avoid a possible segfault. Always restore the PWD on subshell exit and not only if shp->pwd is set. - sh_subshell(): Issue fatal errors in libast's "panic" format. src/cmd/ksh93/tests/builtins.sh: - Adjust a relevant test to run err_exit() outside of the subshell so that any error is counted in the main shell. - Add test for problem 2 described at the top. src/cmd/ksh93/tests/subshell.sh: - Add test for problems 1 and 2 based on reproducer above. Resolves: https://github.com/ksh93/ksh/issues/153
This commit is contained in:
parent
b0a6c1bde5
commit
feaf718f16
4 changed files with 67 additions and 38 deletions
|
|
@ -887,17 +887,21 @@ got=$( { "$SHELL" "$tmp/crash_rhbz1117404.ksh"; } 2>&1)
|
|||
# ======
|
||||
# Segmentation fault when using cd in a subshell, when current directory cannot be determined
|
||||
# https://github.com/ksh93/ksh/issues/153
|
||||
cd "$tmp"
|
||||
mkdir deleted
|
||||
cd deleted
|
||||
mkdir "$tmp/deleted"
|
||||
cd "$tmp/deleted"
|
||||
tmp=$tmp "$SHELL" -c 'cd /; rmdir "$tmp/deleted"'
|
||||
|
||||
exp="subPWD: ${PWD%/deleted}"$'\n'"mainPWD: $PWD"
|
||||
got=$( { "$SHELL" -c '(subshfn() { bad; }; cd ..; echo "subPWD: $PWD"); typeset -f subshfn; echo "mainPWD: $PWD"'; } 2>&1 )
|
||||
[[ $got == "$exp" ]] || err_exit "subshell state not restored after 'cd ..' from deleted PWD" \
|
||||
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
|
||||
|
||||
exp="PWD=$PWD"
|
||||
got=$( { "$SHELL" -c '(cd /; (cd /)); print -r -- "PWD=$PWD"'; } 2>&1 )
|
||||
((!(e = $?))) && [[ $got == "$exp" ]] || err_exit 'failed to restore nonexistent PWD on exiting a virtual subshell' \
|
||||
"(got status $e$( ((e>128)) && print -n / && kill -l "$e"), $(printf %q "$got"))"
|
||||
cd "$tmp"
|
||||
mkdir recreated
|
||||
cd recreated
|
||||
mkdir "$tmp/recreated"
|
||||
cd "$tmp/recreated"
|
||||
tmp=$tmp "$SHELL" -c 'cd /; rmdir "$tmp/recreated"; mkdir "$tmp/recreated"'
|
||||
exp="PWD=$PWD"
|
||||
got=$( { "$SHELL" -c '(cd /); print -r -- "PWD=$PWD"'; } 2>&1 )
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue