Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix LWG-3699 lexically_relative on UNC drive paths (\\?\C:\...) results in a default-constructed value #2867

Merged
merged 4 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions stl/inc/filesystem
Original file line number Diff line number Diff line change
Expand Up @@ -1686,23 +1686,32 @@ namespace filesystem {
return false;
}

_NODISCARD inline path path::lexically_relative(const path& _Base) const {
_NODISCARD inline path path::lexically_relative(const path& _Base_raw) const {
constexpr wstring_view _Dot = L"."sv;
constexpr wstring_view _Dot_dot = L".."sv;

// LWG-3699: `lexically_relative` on UNC drive paths (`\\?\C:\...`) results in a default-constructed value
// This avoids doing any unnecessary copies;
// the return value of `relative_path()` is lifetime-extended if necessary.
const bool _Both_unc_paths = _Is_drive_prefix_with_slash_slash_question(_Text)
&& _Is_drive_prefix_with_slash_slash_question(_Base_raw._Text);
const path& _This = _Both_unc_paths ? relative_path() : *this;
const path& _Base = _Both_unc_paths ? _Base_raw.relative_path() : _Base_raw;

path _Result;

if (root_name() != _Base.root_name() || is_absolute() != _Base.is_absolute()
|| (!has_root_directory() && _Base.has_root_directory()) || _Relative_path_contains_root_name(*this)
|| _Relative_path_contains_root_name(_Base)) {
if (_This.root_name() != _Base.root_name() || _This.is_absolute() != _Base.is_absolute()
|| (!_This.has_root_directory() && _Base.has_root_directory())
|| (_Relative_path_contains_root_name(_This) || _Relative_path_contains_root_name(_Base))) {
strega-nil-ms marked this conversation as resolved.
Show resolved Hide resolved
return _Result;
}

const iterator _This_end = end();

const iterator _This_end = _This.end();
const iterator _Base_begin = _Base.begin();
const iterator _Base_end = _Base.end();

auto _Mismatched = _STD mismatch(begin(), _This_end, _Base_begin, _Base_end);
auto _Mismatched = _STD mismatch(_This.begin(), _This_end, _Base_begin, _Base_end);
iterator& _A_iter = _Mismatched.first;
iterator& _B_iter = _Mismatched.second;

Expand Down
8 changes: 8 additions & 0 deletions tests/std/tests/P0218R1_filesystem/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3145,6 +3145,14 @@ void test_lexically_relative() {

// LWG-3070
EXPECT(path(LR"(\a:\b:)"sv).lexically_relative(LR"(\a:\c:)"sv).native() == LR"()"sv);

// LWG-3699
EXPECT(path(LR"(\\?\a:\meow)"sv).lexically_relative(LR"(\\?\a:\meow)"sv).native() == LR"(.)"sv);
EXPECT(path(LR"(\\?\a:\meow\purr\nyan)"sv).lexically_relative(LR"(\\?\a:\meow)"sv).native() == LR"(purr\nyan)"sv);
EXPECT(path(LR"(\\?\a:\meow)"sv).lexically_relative(LR"(\\?\a:\meow\purr\nyan)"sv).native() == LR"(..\..)"sv);
// UNC/DOS together should return an empty path
EXPECT(path(LR"(a:\meow)"sv).lexically_relative(LR"(\\?\a:\meow)"sv).native().empty());
EXPECT(path(LR"(\\?\a:\meow)"sv).lexically_relative(LR"(a:\meow)"sv).native().empty());
}

void test_lexically_proximate() {
Expand Down