From 5b36ebf7b3ddf4c548b99aa4efc11d166e191b5b Mon Sep 17 00:00:00 2001 From: Hamid Reza Arzaghi Date: Thu, 10 Sep 2020 19:01:31 +0430 Subject: [PATCH 1/4] add test to cover the issue --- tests/std/tests/P0218R1_filesystem/test.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/std/tests/P0218R1_filesystem/test.cpp b/tests/std/tests/P0218R1_filesystem/test.cpp index df813ffa2a1..3a000d5df33 100644 --- a/tests/std/tests/P0218R1_filesystem/test.cpp +++ b/tests/std/tests/P0218R1_filesystem/test.cpp @@ -3570,6 +3570,7 @@ void test_temp_directory_path() { void test_create_directory() { const test_temp_directory tempDir("create_directory"sv); const path p = tempDir.directoryPath / L"__std.c++17.filesystem.create_directory"sv; + const path emptyPath{}; // test happy { @@ -3608,6 +3609,17 @@ void test_create_directory() { } } + // test empty path + { + try { + // create_directory should throw for empty paths + create_directories(emptyPath); + assert(false); + } catch (const filesystem_error&) { + assert(true); + } + } + // test VSO-654638 where create_directory(p, existing_p) was doing copy_symlink behavior { error_code ec; From 44a5bf56a73c885d0145dfcb8f3cc8984e183bb8 Mon Sep 17 00:00:00 2001 From: Hamid Reza Arzaghi Date: Thu, 10 Sep 2020 19:11:13 +0430 Subject: [PATCH 2/4] create_directory throw on empty path --- stl/inc/filesystem | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/stl/inc/filesystem b/stl/inc/filesystem index a2f4bd1dd09..343fb2a1e66 100644 --- a/stl/inc/filesystem +++ b/stl/inc/filesystem @@ -3750,6 +3750,10 @@ namespace filesystem { // FUNCTION create_directories inline bool create_directories(const path& _Path, error_code& _Ec) { + if (_Path.empty()) { + _Throw_fs_error("create_directories", _Make_ec(__std_win_error::_Invalid_parameter), _Path); + } + _Ec.clear(); // for exception safety const wstring& _Text = _Path.native(); wstring _Tmp; From d63731f4a1f1e4f1a7a60d1515f27a7bf462223e Mon Sep 17 00:00:00 2001 From: Hamid Reza Arzaghi Date: Fri, 11 Sep 2020 16:22:11 +0430 Subject: [PATCH 3/4] create_directory throw for empty paths --- stl/inc/filesystem | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stl/inc/filesystem b/stl/inc/filesystem index 343fb2a1e66..da6f941f9c6 100644 --- a/stl/inc/filesystem +++ b/stl/inc/filesystem @@ -3751,7 +3751,8 @@ namespace filesystem { // FUNCTION create_directories inline bool create_directories(const path& _Path, error_code& _Ec) { if (_Path.empty()) { - _Throw_fs_error("create_directories", _Make_ec(__std_win_error::_Invalid_parameter), _Path); + _Ec = _Make_ec(__std_win_error::_Path_not_found); + return false; } _Ec.clear(); // for exception safety From 20fc68dc7123a34a5a8c46cee811f275a14cda9e Mon Sep 17 00:00:00 2001 From: Hamid Reza Arzaghi Date: Wed, 23 Sep 2020 06:41:17 +0330 Subject: [PATCH 4/4] apply suggestions to the test --- tests/std/tests/P0218R1_filesystem/test.cpp | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/tests/std/tests/P0218R1_filesystem/test.cpp b/tests/std/tests/P0218R1_filesystem/test.cpp index 3a000d5df33..0ff4f084c32 100644 --- a/tests/std/tests/P0218R1_filesystem/test.cpp +++ b/tests/std/tests/P0218R1_filesystem/test.cpp @@ -3570,7 +3570,6 @@ void test_temp_directory_path() { void test_create_directory() { const test_temp_directory tempDir("create_directory"sv); const path p = tempDir.directoryPath / L"__std.c++17.filesystem.create_directory"sv; - const path emptyPath{}; // test happy { @@ -3609,17 +3608,6 @@ void test_create_directory() { } } - // test empty path - { - try { - // create_directory should throw for empty paths - create_directories(emptyPath); - assert(false); - } catch (const filesystem_error&) { - assert(true); - } - } - // test VSO-654638 where create_directory(p, existing_p) was doing copy_symlink behavior { error_code ec; @@ -3681,6 +3669,11 @@ void test_create_dirs_and_remove_all() { remove_all(badPath, ec); EXPECT(good(ec)); + // test GH-1283 create_directories() should throw for empty paths + EXPECT(throws_filesystem_error([] { create_directories(path{}); }, "create_directories", path{})); + EXPECT(create_directories(path{}, ec) == false); + EXPECT(bad(ec)); + // test that normalization isn't done first auto dots = r / L"a/../b/../c"sv; EXPECT(create_directories(dots));