Skip to content

<filesystem>: system_error thrown from copy_file #927

@fkutzner

Description

@fkutzner

Describe the bug
When a std::filesystem::path object p contains characters that cannot be represented in the current code page, p.string() throws a system_error because of the conversion failure. This may cause a system_error exception being thrown from a filesystem_error constructor.

These system_error exceptions are not catched in the filesystem_error constructor or the std::filesystem::copy_file function. I think the standard intends copy_file to still throw a filesystem_error in this situation (http://eel.is/c++draft/fs.err.report#2.1). Other functions constructing filesystem_error exceptions are likely affected as well.

Command-line test case
Set the system locale to English (United States)

C:\repro> type fsbug.cpp
#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main() {
  std::wstring const alef = L"\u05D0";

  try {
    fs::copy_file(fs::path{ alef }, fs::path{ L"Foo" });
  }
  catch (fs::filesystem_error const& e) {
    std::cout << e.what() << "\n";
  }
  catch (std::system_error const& e) {
    std::cout << "This line should not be reachable\n";
    std::cout << e.what() << "\n";
  }
  return 0;
}

C:\repro> cl.exe /D _UNICODE /D UNICODE /Zc:wchar_t /std:c++17 /EHsc fsbug.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.24.28315 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

fsbug.cpp
Microsoft (R) Incremental Linker Version 14.24.28315.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:fsbug.exe
fsbug.obj

C:\repro> .\fsbug.exe
This line should not be reachable
No mapping for the Unicode character exists in the target multi-byte code page.

Expected behavior
filesystem_error should be thrown by copy_file, not system_error. I think constructing a filesystem_error with paths not representable in the current locale should not cause the construction of the filesystem_error object to fail.

STL version

Microsoft Visual Studio Professional 2019
Version 16.4.3 

(Edit: minor clarifications)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions