Always pass in the working directory in path_get_cdpath

If the user is in a directory which has been unlinked, it is possible
for the path .. to not exist, relative to the working directory.
Always pass in the working directory (potentially virtual) to
path_get_cdpath; this ensures we check absolute paths and are immune
from issues if the working directory has been unlinked.

Also introduce a new function path_normalize_for_cd which normalizes the
"join point" of a path and a working directory. This allows us to 'cd' out of
a non-existent directory, but not cd into such a directory.

Fixes #5341
This commit is contained in:
ridiculousfish
2018-11-17 18:02:28 -08:00
parent 1ab84ac62a
commit a8ce7bad7b
8 changed files with 76 additions and 19 deletions

View File

@@ -4916,6 +4916,19 @@ void test_normalize_path() {
do_test(normalize_path(L"foo/../foo") == L"foo");
do_test(normalize_path(L"foo/../foo/") == L"foo");
do_test(normalize_path(L"foo/././bar/.././baz") == L"foo/baz");
do_test(path_normalize_for_cd(L"/", L"..") == L"/..");
do_test(path_normalize_for_cd(L"/abc/", L"..") == L"/");
do_test(path_normalize_for_cd(L"/abc/def/", L"..") == L"/abc");
do_test(path_normalize_for_cd(L"/abc/def/", L"../..") == L"/");
do_test(path_normalize_for_cd(L"/abc///def/", L"../..") == L"/");
do_test(path_normalize_for_cd(L"/abc///def/", L"../..") == L"/");
do_test(path_normalize_for_cd(L"/abc///def///", L"../..") == L"/");
do_test(path_normalize_for_cd(L"/abc///def///", L"..") == L"/abc");
do_test(path_normalize_for_cd(L"/abc///def///", L"..") == L"/abc");
do_test(path_normalize_for_cd(L"/abc/def/", L"./././/..") == L"/abc");
do_test(path_normalize_for_cd(L"/abc/def/", L"../../../") == L"/../");
do_test(path_normalize_for_cd(L"/abc/def/", L"../ghi/..") == L"/abc/ghi/..");
}
/// Main test.