Skip to content

<filesystem>: temp_directory_path(error_code&) doesn't return an empty path for errors #5557

Open
@StephanTLavavej

Description

@StephanTLavavej

WG21-N5008 [fs.op.temp.dir.path]/3:

Returns: The path p. The signature with argument ec returns path() if an error occurs.

That doesn't appear to be what we do:

STL/stl/inc/filesystem

Lines 4042 to 4056 in 7841cf8

_EXPORT_STD _NODISCARD inline path temp_directory_path(error_code& _Ec) {
// get a location suitable for temporary storage, and verify that it is a directory
_Ec.clear(); // for exception safety
path _Result;
_Result._Text.resize(__std_fs_temp_path_max);
const auto _Temp_result = __std_fs_get_temp_path(_Result._Text.data());
_Result._Text.resize(_Temp_result._Size);
if (_Temp_result._Error == __std_win_error::_Max) { // path could be retrieved, but was not a directory
_Ec = _STD make_error_code(errc::not_a_directory);
} else {
_Ec = _Make_ec(_Temp_result._Error);
}
return _Result;
}

STL/stl/src/filesystem.cpp

Lines 807 to 835 in 7841cf8

[[nodiscard]] _Success_(return._Error == __std_win_error::_Success) __std_ulong_and_error
__stdcall __std_fs_get_temp_path(_Out_writes_z_(__std_fs_temp_path_max) wchar_t* const _Target) noexcept {
// calls GetTempPath2W if available (Win11+), else calls GetTempPathW
// If getting the path failed, returns 0 size; otherwise, returns the size of the
// expected directory. If the path could be resolved to an existing directory,
// returns __std_win_error::_Success; otherwise, returns __std_win_error::_Max.
const auto _Size = _Stl_GetTempPath2W(__std_fs_temp_path_max, _Target);
if (_Size == 0) {
return {0, __std_win_error{GetLastError()}};
}
// Effects: If exists(p) is false or is_directory(p) is false, an error is reported
const DWORD _Attributes = GetFileAttributesW(_Target);
if (_Attributes == INVALID_FILE_ATTRIBUTES || (_Attributes & FILE_ATTRIBUTE_DIRECTORY) == 0u) {
return {_Size, __std_win_error::_Max};
}
if ((_Attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0u) {
__std_fs_file_handle _Handle;
const auto _Last_error = __std_fs_open_handle(
&_Handle, _Target, __std_access_rights::_File_read_attributes, __std_fs_file_flags::_Backup_semantics);
__std_fs_close_handle(_Handle);
if (_Last_error != __std_win_error::_Success) {
return {_Size, __std_win_error::_Max};
}
}
return {_Size, __std_win_error::_Success};
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfilesystemC++17 filesystem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions