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

<system_error>: explicitly pass the system locale ID for system_category messages #2669

Merged
merged 3 commits into from
May 1, 2022

Conversation

fsb4000
Copy link
Contributor

@fsb4000 fsb4000 commented Apr 24, 2022

Fixes #2451

type main.cpp
#include <clocale>
#include <iostream>
#include <string>

#include <Windows.h>

int main() {
    setlocale(LC_ALL, "");
    auto file_handle(CreateFile(
        L"D:\\non_exist_file.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
    if (file_handle == INVALID_HANDLE_VALUE) {
        const auto error_code = GetLastError();
        std::string errmsg    = std::system_category().message(error_code); // Sometimes the string only contains "???"
        std::cout << errmsg << '\n';
    }
}

If we have different “user language id” and “system language id” (this is how we can reach it: #2451 (comment)) FormatMessageA can choose “user language id” instead of “system language id” if we pass 0.

Before

>main
??????? ?? ??????? ????? ????????? ????.

After

>main
The system cannot find the path specified.

@fsb4000 fsb4000 requested a review from a team as a code owner April 24, 2022 08:42
@StephanTLavavej StephanTLavavej added the bug Something isn't working label Apr 26, 2022
@StephanTLavavej StephanTLavavej self-assigned this Apr 27, 2022
stl/src/syserror_import_lib.cpp Outdated Show resolved Hide resolved
@StephanTLavavej StephanTLavavej removed their assignment Apr 28, 2022
@StephanTLavavej StephanTLavavej self-assigned this Apr 30, 2022
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit fa51112 into microsoft:main May 1, 2022
@StephanTLavavej
Copy link
Member

Thanks for investigating and fixing this runtime correctness bug! 🕵️ 🐞 😻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

<system_error>: FormatMessageA needs be used in a different way
5 participants