Skip to content

Commit

Permalink
Fix crash when cd-ing away from a directory that's been removed.
Browse files Browse the repository at this point in the history
Tickled by Aboriginal Linux.

Addresses issue #58.
  • Loading branch information
Andy Chu committed Jan 1, 2018
1 parent 0573fd4 commit 7103613
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions core/builtin.py
Expand Up @@ -589,8 +589,12 @@ def Cd(argv, mem, dir_stack):
# can't even set it at all.)
raise AssertionError('Invalid OLDPWD')

# Save OLDPWD.
state.SetGlobalString(mem, 'OLDPWD', os.getcwd())
# NOTE: We can't call os.getcwd() because it can raise OSError if the
# directory was removed (ENOENT.)

pwd = mem.GetVar('PWD')
assert pwd.tag == value_e.Str, pwd # TODO: Need a general scheme to avoid
state.SetGlobalString(mem, 'OLDPWD', pwd.s)
try:
os.chdir(dest_dir)
except OSError as e:
Expand Down
13 changes: 13 additions & 0 deletions spec/builtins.test.sh
Expand Up @@ -41,6 +41,19 @@ echo status=$?
# stdout: status=1
# OK dash/mksh stdout: status=2

### cd away from dir that was deleted
dir=$TMP/cd-nonexistent
mkdir -p $dir
cd $dir
rmdir $dir
cd $TMP
echo $(basename $OLDPWD)
echo status=$?
## STDOUT:
cd-nonexistent
status=0
## END

### pushd/popd
set -o errexit
cd /
Expand Down

0 comments on commit 7103613

Please sign in to comment.