From 59cb2d02a85a806884a99e39d7b1e5d4d35c129e Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 18 Feb 2019 13:11:01 -0800 Subject: [PATCH] Only inherit a PWD if it resolves to "." Fixes #5647 --- src/env.cpp | 12 +++++++----- tests/cd.in | 14 ++++++++++++++ tests/cd.out | 2 ++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/env.cpp b/src/env.cpp index 5b840859a..6da436aba 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -1014,11 +1014,13 @@ void env_init(const struct config_paths_t *paths /* or NULL */) { } // initialize the PWD variable if necessary - // Note we may inherit a virtual PWD that doesn't match what getcwd would return; respect that. - // Note we treat PWD as read-only so it was not set in vars. - const char *incoming_pwd = getenv("PWD"); - if (incoming_pwd && incoming_pwd[0]) { - vars.set_one(L"PWD", ENV_EXPORT | ENV_GLOBAL, str2wcstring(incoming_pwd)); + // Note we may inherit a virtual PWD that doesn't match what getcwd would return; respect that + // if and only if it matches getcwd (#5647). Note we treat PWD as read-only so it was not set in + // vars. + const char *incoming_pwd_cstr = getenv("PWD"); + wcstring incoming_pwd = incoming_pwd_cstr ? str2wcstring(incoming_pwd_cstr) : wcstring{}; + if (!incoming_pwd.empty() && paths_are_same_file(incoming_pwd, L".")) { + vars.set_one(L"PWD", ENV_EXPORT | ENV_GLOBAL, incoming_pwd); } else { vars.set_pwd_from_getcwd(); } diff --git a/tests/cd.in b/tests/cd.in index a537b1ea8..2d02d269c 100644 --- a/tests/cd.in +++ b/tests/cd.in @@ -47,8 +47,22 @@ mkdir -p $base/realhome set fish_path $PWD/../test/root/bin/fish ln -s $base/realhome $base/linkhome cd $base/linkhome +set -l real_getcwd (pwd -P) env HOME=$base/linkhome $fish_path -c 'echo PWD is $PWD' +# Do not inherit a virtual PWD that fails to resolve to getcwd (#5647) + +env HOME=$base/linkhome PWD=/tmp $fish_path -c 'echo $PWD' | read output_pwd +test (realpath $output_pwd) = $real_getcwd +and echo "BogusPWD test 1 succeeded" +or echo "BogusPWD test 1 failed: $output_pwd vs $real_getcwd" + +env HOME=$base/linkhome PWD=/path/to/nowhere $fish_path -c 'echo $PWD' | read output_pwd +test (realpath $output_pwd) = $real_getcwd +and echo "BogusPWD test 2 succeeded" +or echo "BogusPWD test 2 failed: $output_pwd vs $real_getcwd" + + # cd back before removing the test directory again. cd $oldpwd rm -Rf $base diff --git a/tests/cd.out b/tests/cd.out index 73081263d..8fd581f17 100644 --- a/tests/cd.out +++ b/tests/cd.out @@ -21,3 +21,5 @@ cd: #################### # Virtual PWD inheritance PWD is /tmp/cdcomp_test/linkhome +BogusPWD test 1 succeeded +BogusPWD test 2 succeeded