Skip to content

<system_error>: system_error doesn't honor the current threads's locale  #2882

Description

@fsb4000

Describe the bug
"Is there a way to make system_error to honor the current threads’s locale ?
This always prints an error-message according to my default locale."

Command-line test case

C:\Temp>type repro.cpp
#include <clocale>
#include <iostream>
#include <string>

#include <Windows.h>

int main() {
    setlocale(LC_ALL, "");
    SetThreadLocale(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT));
    std::string errmsg = std::system_category().message(1);
    std::cout << errmsg << '\n';
}

C:\Temp>cl /nologo /EHsc /W4 /WX repro.cpp
repro.cpp

C:\Temp>.\repro.exe
Неверная функция.

Expected behavior
The message should be in English. (My System Language is Russian)
The program should have a way to change locale of system_error messages.

STL version
git commit hash: ef62d3f

Additional context
DevCom-10098476

Currently we use LOCALE_NAME_SYSTEM_DEFAULT so we don't honor the current threads's locale.

_NODISCARD size_t __CLRCALL_PURE_OR_STDCALL __std_system_error_allocate_message(
const unsigned long _Message_id, char** const _Ptr_str) noexcept {
// convert to name of Windows error, return 0 for failure, otherwise return number of chars in buffer
// __std_system_error_deallocate_message should be called even if 0 is returned
// pre: *_Ptr_str == nullptr
DWORD _Lang_id;
const int _Ret = GetLocaleInfoEx(LOCALE_NAME_SYSTEM_DEFAULT, LOCALE_ILANGUAGE | LOCALE_RETURN_NUMBER,
reinterpret_cast<LPWSTR>(&_Lang_id), sizeof(_Lang_id) / sizeof(wchar_t));
if (_Ret == 0) {
_Lang_id = 0;
}
const unsigned long _Chars =
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, _Message_id, _Lang_id, reinterpret_cast<char*>(_Ptr_str), 0, nullptr);
return _CSTD __std_get_string_size_without_trailing_whitespace(*_Ptr_str, _Chars);
}

We can't use GetThreadLocale because it doesn't work with UWP.
Minimum supported client | Windows 2000 Professional [desktop apps only]

I recently changed the function but previous version didn't respect the current threads's locale either: https://godbolt.org/z/37f44nahM

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions