| Message ID | 20260110104314.1943-1-zheng.xianyuwang@gmail.com |
|---|---|
| State | Committed |
| Headers |
Return-Path: <gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 43BA74BA2E1E for <patchwork@sourceware.org>; Sat, 10 Jan 2026 10:46:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 43BA74BA2E1E Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=VN24wGaz X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by sourceware.org (Postfix) with ESMTPS id 30B1F4BA2E04 for <gcc-patches@gcc.gnu.org>; Sat, 10 Jan 2026 10:44:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 30B1F4BA2E04 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 30B1F4BA2E04 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768041879; cv=none; b=aFbge5nkosHXttqFMuWsBPA8/nG+MTYaFlR8HL+UyAbDexn82JHNDFQPV/rejvja13RDzmzUTKQTV4lnjnDUko6jJVfb6CKFIgifmMw1q6G2YCH7rsi/rVKtuEsV1jCRhdEMI+6+bPzoKFFJY75GvTOQ6UdY3n+K4FNMBJZCzNU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1768041879; c=relaxed/simple; bh=A5lurBzHXJDJVuyNPHGkNQz6OFJvxoS9XNhj4sXbevg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=A3KLsrs5WPcM4JU+shV112sEF0xdvkfVsDtivMWFcQNbmC3aGuVz/PEhwNdNOuXpVAxvoLUY0EtOzc/okKZokP08BPk6TjCl6rG6XCVoS0HnZEZoG8NcNYpiu0NQ69zvR4LfrhzU5o6WtSOdlyufrz7GXvZrkUjZMxwqdmC6EbA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 30B1F4BA2E04 Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2a0d06ffa2aso37355235ad.3 for <gcc-patches@gcc.gnu.org>; Sat, 10 Jan 2026 02:44:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1768041878; x=1768646678; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ru3sVbVNiKPuUkU+2sG1J+024Y62Nuv260XST5ilzzQ=; b=VN24wGaz2Eb0PdsLPpgReS2ehB/vsxh1Fw9YlVp8mPvKfuWNNasVqA80up/yVxMNF8 nP+S1E/5xWIzXw0sLsHrJ6rVFfSwz7G35I9Eb9vs/6T+W1NPmvGE9WooLmbYgscbc6vU Ofj4sf2THOn2423vhvIXVQvdvfhDEInwgZHlDTxsStVqrf2+x3+241vrlzHk+AWmKSxq lwQIl/jK9tp+NWUbJ6+P5Fo6J1wYeirxHV1iIM8H+oecIYK9Em+KyM45xY5bI7I4seZk Nxgi7ej/7Kt47Kh5TqNN1d8uq4JzSdRF4XA1pdiNodBWbNlEwDGDAvOi9WphjzbwYdrO xalw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768041878; x=1768646678; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ru3sVbVNiKPuUkU+2sG1J+024Y62Nuv260XST5ilzzQ=; b=PlDP1Nz4/62e6mbO4A7ScjiNqcFqad5bR1IICVykwaWm9dHYEhlv7Y/mWst7mXvmw2 epolaLO72rgGyocytDS/wySShvQKM3wtTz4J9SW5Cx71GbIBdysh+JOzqko1u9G9edCd Zy2CplVEHsw1ZrTqKh/H22wzGy9YxMs9/ofT5C1tszvA7pKYTwf1j2CzyOEafU1H4/bB 6eWeQBQNwws/wgymqdnGOI+q9sVc3sCrcRfHWM0J1/Ei3xCiTZu1r483YVawS8gYTwu0 ZEOb++86P67BX7GAlUYOHJFKViqXDdLUGEDHhXlP1gdJU9alpRFxnjMlNgOVasDUQhfo TT8Q== X-Forwarded-Encrypted: i=1; AJvYcCWyKGzdrwmq9RnT9Jd3k9uT4/Ael6Fpl4WAkIqT8lAxDHJWKcGmTUJAfEewuMaByDk+noEpoGXonjfCAg==@gcc.gnu.org X-Gm-Message-State: AOJu0YxXkRpap958RBJAAKitm0dcS9G9/vH3xjpnZYPhdXeGPIMwRcFX ntjH4dLpR7A36xO5ddiNl4WI3uEx79O2lxayDenMb8Lzn4EySaQ6BA1b9miI5hZrEw58oQ== X-Gm-Gg: AY/fxX6pEXDz0dLtfJX8toahyliOHOwfJqhrCe15y0fSf64qi1jjMSAfNkOsTbzOCxm lE1OEbbCHpq+73yMd4YbKFgQZY/FDejHUj5LALAKJX1SKqCVGtOq8808ayqlEBxuLnLWSmgzxeo F6Q9LuDgmUEzpS59UMU9wIRGVzrK1xsSjVQTj1ghqCH8VGgHC8BnRLmelNRFoty/4q3MZifsLMj 6k4LmW+WB9dG5atN+Cx7RpCrIaIYTcViNBGWNrr91NQ/PLvAetECSIInumJHhTltNHnUugMF6AU 582zgtuOhR6jFNtN9zproJgRDrvvhOfLVyt2FIXkDejBG2njQyf/nqBXgtM5Ln9FzAzXRYYHhRg b/rvc5lwGVMERq4wDv3fsg9tQbzkrVQSV/FqXU/wyYDfmhOgSQY9sEnT3HRgBzv4dNYVjXXu6oK 471CdlEu3wILnLTu3ZSqhIhvQ1PU3edh6g8DgK1gH38A4= X-Google-Smtp-Source: AGHT+IHIJCUXsSVPGqEO2rhFZVg1RQMm7R8XMC0k7be+XOG4b5cARv7k39E9VToijeqbZBzTCK/Q2w== X-Received: by 2002:a17:90b:4a47:b0:340:d81d:7874 with SMTP id 98e67ed59e1d1-34f68cd6b56mr12363600a91.26.1768041878019; Sat, 10 Jan 2026 02:44:38 -0800 (PST) Received: from DESKTOP-E2R7LGL ([150.129.80.204]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-34f5f6db356sm12733737a91.0.2026.01.10.02.44.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Jan 2026 02:44:37 -0800 (PST) From: Wang Jinghao <zheng.xianyuwang@gmail.com> To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Cc: Wang Jinghao <zheng.xianyuwang@gmail.com> Subject: [PATCH v2] libstdc++: Fix std::system_category().message(int) on mingw32 target Date: Sat, 10 Jan 2026 18:43:14 +0800 Message-ID: <20260110104314.1943-1-zheng.xianyuwang@gmail.com> X-Mailer: git-send-email 2.52.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org |
| Series |
[v2] libstdc++: Fix std::system_category().message(int) on mingw32 target
|
|
Commit Message
Wang Jinghao
Jan. 10, 2026, 10:43 a.m. UTC
On the mingw32 target, std::system_category().message(int) uses FormatMessage api to format error messages. When the error message contains insert sequences, it is unsafe not to use the FORMAT_MESSAGE_OGNORE_INSERTS flag, as seen at: https://devblogs.microsoft.com/oldnewthing/20071128-00/?p=24353 The output of FormatMessage ends with "\r\n" and includes a Full stop character used by the current thread's UI language. Now, we will remove "\r\n" and any trailing '.' from the output in any language environment. In the testsuite for std::system_category().message(int), we first switch the thread UI language to en-US tomeet expectations in any language environment. libstdc++-v3/ChangeLog: * src/c++11/system_error.cc (system_error_category) [_WIN32]: Fix typo in __MINGW32__ macro name. Adjust behavior on the mingw32 target. Use FormatMessageA function instead of FormatMessage macro. * testsuite/19_diagnostics/error_category/system_category.cc: Adjust behavior on the mingw32 target. --- libstdc++-v3/src/c++11/system_error.cc | 21 ++++++++++--------- .../error_category/system_category.cc | 18 +++++++++++++--- 2 files changed, 26 insertions(+), 13 deletions(-)
Comments
On Sat, 10 Jan 2026 at 10:45, Wang Jinghao <zheng.xianyuwang@gmail.com> wrote: > > On the mingw32 target, std::system_category().message(int) uses > FormatMessage api to format error messages. When the error message contains > insert sequences, it is unsafe not to use the FORMAT_MESSAGE_OGNORE_INSERTS > flag, as seen at: > https://devblogs.microsoft.com/oldnewthing/20071128-00/?p=24353 > > The output of FormatMessage ends with "\r\n" and includes a Full stop character > used by the current thread's UI language. Now, we will remove "\r\n" and > any trailing '.' from the output in any language environment. > > In the testsuite for std::system_category().message(int), we first switch the > thread UI language to en-US tomeet expectations in any language environment. Thanks, the patch looks good now. > libstdc++-v3/ChangeLog: > > * src/c++11/system_error.cc (system_error_category) [_WIN32]: > Fix typo in __MINGW32__ macro name. > Adjust behavior on the mingw32 target. The two lines above relate to the error_category/system_category.cc test, not the src/c++11/system_category.cc source file, right? > Use FormatMessageA function instead of FormatMessage macro. > * testsuite/19_diagnostics/error_category/system_category.cc: > Adjust behavior on the mingw32 target. I've just noticed that there's no Signed-off-by trailer in the patch. Do you have a copyright assignment filed with the FSF? As documented at https://gcc.gnu.org/contribute.html#legal we need either a copyright assignment, or DCO sign-off. If you don't have a copyrigh assignment, could you please add the Signed-off-by trailer if you are willing and able to agree to the terms of https://gcc.gnu.org/dco.html ? Thanks > --- > libstdc++-v3/src/c++11/system_error.cc | 21 ++++++++++--------- > .../error_category/system_category.cc | 18 +++++++++++++--- > 2 files changed, 26 insertions(+), 13 deletions(-) > > diff --git a/libstdc++-v3/src/c++11/system_error.cc b/libstdc++-v3/src/c++11/system_error.cc > index 548c88fcfde..7e3a3a0c904 100644 > --- a/libstdc++-v3/src/c++11/system_error.cc > +++ b/libstdc++-v3/src/c++11/system_error.cc > @@ -161,22 +161,23 @@ namespace > #if defined(_WIN32) && !defined(__CYGWIN__) > char* buf = nullptr; > auto len > - = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM > - | FORMAT_MESSAGE_ALLOCATE_BUFFER, > - nullptr, > - i, > - LANG_USER_DEFAULT, > - reinterpret_cast<LPTSTR>(&buf), > - 0, > - nullptr); > + = FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM > + | FORMAT_MESSAGE_ALLOCATE_BUFFER > + | FORMAT_MESSAGE_IGNORE_INSERTS, > + nullptr, > + i, > + LANG_USER_DEFAULT, > + reinterpret_cast<LPTSTR>(&buf), > + 0, > + nullptr); > if (len > 0) > { > struct deleter { > void operator()(void* p) const { ::LocalFree(p); } > }; > std::unique_ptr<char[], deleter> guard(buf); > - if (len > 3 && !__builtin_memcmp(buf + len - 3, ".\r\n", 3)) [[likely]] > - len -= 3; > + if (len > 2 && !__builtin_memcmp (buf + len - 2, "\r\n", 2)) [[likely]] > + len -= 2 + (buf[len - 3] == '.'); > return string(buf, len); > } > return string("Unknown error code"); > diff --git a/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc b/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc > index 250d34f9bb5..03577234c43 100644 > --- a/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc > +++ b/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc > @@ -21,6 +21,11 @@ > #include <locale> > #include <testsuite_hooks.h> > > +#if defined __MINGW32__ || defined __MINGW64__ > +#define WIN32_LEAN_AND_MEAN > +#include <windows.h> > +#endif > + > void > test01() > { > @@ -34,7 +39,7 @@ test02() > const std::error_category& cat = std::system_category(); > std::error_condition cond; > > -#if defined __MING32__ || defined __MINGW64__ > +#if defined __MINGW32__ || defined __MINGW64__ > cond = cat.default_error_condition(8); // ERROR_NOT_ENOUGH_MEMORY > VERIFY( cond.value() == ENOMEM ); > VERIFY( cond.category() == std::generic_category() ); > @@ -112,9 +117,16 @@ test03() > // set "C" locale to get expected message > auto loc = std::locale::global(std::locale::classic()); > > -#if defined __MING32__ || defined __MINGW64__ > +#if defined __MINGW32__ || defined __MINGW64__ > + // On Windows, set thread preferred UI languages to "en-US" > + // to get expected message > + ULONG num_langs = 1; > + SetThreadPreferredUILanguages(MUI_LANGUAGE_NAME, L"en-US\0", &num_langs); > + > std::string msg = std::system_category().message(5); // ERROR_ACCESS_DENIED > - VERIFY(msg == "Access denied"); > + VERIFY(msg == "Access is denied"); > + > + SetThreadPreferredUILanguages(MUI_RESET_FILTERS, nullptr, nullptr); > #else > std::string msg = std::system_category().message(EBADF); > VERIFY( msg.find("file") != std::string::npos ); > -- > 2.52.0 >
On Sat, 10 Jan 2026 at 12:17, Jonathan Wakely <jwakely.gcc@gmail.com> wrote: > > On Sat, 10 Jan 2026 at 10:45, Wang Jinghao <zheng.xianyuwang@gmail.com> wrote: > > > > On the mingw32 target, std::system_category().message(int) uses > > FormatMessage api to format error messages. When the error message contains > > insert sequences, it is unsafe not to use the FORMAT_MESSAGE_OGNORE_INSERTS > > flag, as seen at: > > https://devblogs.microsoft.com/oldnewthing/20071128-00/?p=24353 > > > > The output of FormatMessage ends with "\r\n" and includes a Full stop character > > used by the current thread's UI language. Now, we will remove "\r\n" and > > any trailing '.' from the output in any language environment. > > > > In the testsuite for std::system_category().message(int), we first switch the > > thread UI language to en-US tomeet expectations in any language environment. > > Thanks, the patch looks good now. > > > libstdc++-v3/ChangeLog: > > > > * src/c++11/system_error.cc (system_error_category) [_WIN32]: > > Fix typo in __MINGW32__ macro name. > > Adjust behavior on the mingw32 target. > > The two lines above relate to the error_category/system_category.cc > test, not the src/c++11/system_category.cc source file, right? > > > Use FormatMessageA function instead of FormatMessage macro. > > * testsuite/19_diagnostics/error_category/system_category.cc: > > Adjust behavior on the mingw32 target. > > I've just noticed that there's no Signed-off-by trailer in the patch. > Do you have a copyright assignment filed with the FSF? > > As documented at https://gcc.gnu.org/contribute.html#legal we need > either a copyright assignment, or DCO sign-off. > > If you don't have a copyrigh assignment, could you please add the > Signed-off-by trailer if you are willing and able to agree to the > terms of https://gcc.gnu.org/dco.html ? Actually, don't worry, I don't think this change can be considered "legally significant", because the changes are small except for some whitespace changes and typo fixes. So I'll push this to the git repo - thanks for fixing it!
On Sat, 10 Jan 2026 at 12:24, Jonathan Wakely <jwakely.gcc@gmail.com> wrote: > > On Sat, 10 Jan 2026 at 12:17, Jonathan Wakely <jwakely.gcc@gmail.com> wrote: > > > > On Sat, 10 Jan 2026 at 10:45, Wang Jinghao <zheng.xianyuwang@gmail.com> wrote: > > > > > > On the mingw32 target, std::system_category().message(int) uses > > > FormatMessage api to format error messages. When the error message contains > > > insert sequences, it is unsafe not to use the FORMAT_MESSAGE_OGNORE_INSERTS > > > flag, as seen at: > > > https://devblogs.microsoft.com/oldnewthing/20071128-00/?p=24353 > > > > > > The output of FormatMessage ends with "\r\n" and includes a Full stop character > > > used by the current thread's UI language. Now, we will remove "\r\n" and > > > any trailing '.' from the output in any language environment. > > > > > > In the testsuite for std::system_category().message(int), we first switch the > > > thread UI language to en-US tomeet expectations in any language environment. > > > > Thanks, the patch looks good now. > > > > > libstdc++-v3/ChangeLog: > > > > > > * src/c++11/system_error.cc (system_error_category) [_WIN32]: > > > Fix typo in __MINGW32__ macro name. > > > Adjust behavior on the mingw32 target. > > > > The two lines above relate to the error_category/system_category.cc > > test, not the src/c++11/system_category.cc source file, right? > > > > > Use FormatMessageA function instead of FormatMessage macro. > > > * testsuite/19_diagnostics/error_category/system_category.cc: > > > Adjust behavior on the mingw32 target. > > > > I've just noticed that there's no Signed-off-by trailer in the patch. > > Do you have a copyright assignment filed with the FSF? > > > > As documented at https://gcc.gnu.org/contribute.html#legal we need > > either a copyright assignment, or DCO sign-off. > > > > If you don't have a copyrigh assignment, could you please add the > > Signed-off-by trailer if you are willing and able to agree to the > > terms of https://gcc.gnu.org/dco.html ? > > Actually, don't worry, I don't think this change can be considered > "legally significant", because the changes are small except for some > whitespace changes and typo fixes. > > So I'll push this to the git repo - thanks for fixing it! Hmm, when I test this with mingw-w64 under Wine, I get: /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc 01ec:fixme:nls:RtlSetThreadPreferredUILanguages 8, 00000001400A8122, 000000000021FE8C /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc:127: void test03(): Assertion 'msg == "Access is denied"' failed. FAIL: /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc Inspecting the 'msg' string, the content is "Access denied", which is what we have on trunk today. Can you confirm whether it should be "Access is denied" (as in your patch) or "Access denied"?
On 2026/1/10 20:50, Jonathan Wakely wrote: > On Sat, 10 Jan 2026 at 12:24, Jonathan Wakely <jwakely.gcc@gmail.com> wrote: >> On Sat, 10 Jan 2026 at 12:17, Jonathan Wakely <jwakely.gcc@gmail.com> wrote: >>> On Sat, 10 Jan 2026 at 10:45, Wang Jinghao <zheng.xianyuwang@gmail.com> wrote: >>>> On the mingw32 target, std::system_category().message(int) uses >>>> FormatMessage api to format error messages. When the error message contains >>>> insert sequences, it is unsafe not to use the FORMAT_MESSAGE_OGNORE_INSERTS >>>> flag, as seen at: >>>> https://devblogs.microsoft.com/oldnewthing/20071128-00/?p=24353 >>>> >>>> The output of FormatMessage ends with "\r\n" and includes a Full stop character >>>> used by the current thread's UI language. Now, we will remove "\r\n" and >>>> any trailing '.' from the output in any language environment. >>>> >>>> In the testsuite for std::system_category().message(int), we first switch the >>>> thread UI language to en-US tomeet expectations in any language environment. >>> Thanks, the patch looks good now. >>> >>>> libstdc++-v3/ChangeLog: >>>> >>>> * src/c++11/system_error.cc (system_error_category) [_WIN32]: >>>> Fix typo in __MINGW32__ macro name. >>>> Adjust behavior on the mingw32 target. >>> The two lines above relate to the error_category/system_category.cc >>> test, not the src/c++11/system_category.cc source file, right? Sorry, I made a mistake. The correction for the __MINGW32__ macro is in error_category/system_category.cc. >>>> Use FormatMessageA function instead of FormatMessage macro. >>>> * testsuite/19_diagnostics/error_category/system_category.cc: >>>> Adjust behavior on the mingw32 target. >>> I've just noticed that there's no Signed-off-by trailer in the patch. >>> Do you have a copyright assignment filed with the FSF? >>> >>> As documented at https://gcc.gnu.org/contribute.html#legal we need >>> either a copyright assignment, or DCO sign-off. >>> >>> If you don't have a copyrigh assignment, could you please add the >>> Signed-off-by trailer if you are willing and able to agree to the >>> terms of https://gcc.gnu.org/dco.html ? >> Actually, don't worry, I don't think this change can be considered >> "legally significant", because the changes are small except for some >> whitespace changes and typo fixes. >> >> So I'll push this to the git repo - thanks for fixing it! Thank you for your reminder! > Hmm, when I test this with mingw-w64 under Wine, I get: > > /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc > 01ec:fixme:nls:RtlSetThreadPreferredUILanguages 8, 00000001400A8122, > 000000000021FE8C > /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc:127: > void test03(): Assertion 'msg == "Access is denied"' > failed. > FAIL: /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc > > Inspecting the 'msg' string, the content is "Access denied", which is > what we have on trunk today. > > Can you confirm whether it should be "Access is denied" (as in your > patch) or "Access denied"? I apologize that I haven't tested it on Wine, but the the error message I encountered on my Windows 11 Version 10.0.26200 system was "Access is denied," which is the same as the one recorded in Microsoft Docs, see here: https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src/Debug/system-error-codes--0-499-.md or https://winprotocoldoc.z19.web.core.windows.net/MS-ERREF/%5bMS-ERREF%5d.pdf This is probably because Wine is implemented differently than Microsoft Windows. :( Perhaps we also need to add checks for the Wine environment, like those found at https://web.archive.org/web/20131028024505/https://www.winehq.org/pipermail/wine-devel/2008-September/069387.html
On Sat, 10 Jan 2026 at 13:28, Wang Jinghao <zheng.xianyuwang@gmail.com> wrote: > > > On 2026/1/10 20:50, Jonathan Wakely wrote: > > On Sat, 10 Jan 2026 at 12:24, Jonathan Wakely <jwakely.gcc@gmail.com> wrote: > >> On Sat, 10 Jan 2026 at 12:17, Jonathan Wakely <jwakely.gcc@gmail.com> wrote: > >>> On Sat, 10 Jan 2026 at 10:45, Wang Jinghao <zheng.xianyuwang@gmail.com> wrote: > >>>> On the mingw32 target, std::system_category().message(int) uses > >>>> FormatMessage api to format error messages. When the error message contains > >>>> insert sequences, it is unsafe not to use the FORMAT_MESSAGE_OGNORE_INSERTS > >>>> flag, as seen at: > >>>> https://devblogs.microsoft.com/oldnewthing/20071128-00/?p=24353 > >>>> > >>>> The output of FormatMessage ends with "\r\n" and includes a Full stop character > >>>> used by the current thread's UI language. Now, we will remove "\r\n" and > >>>> any trailing '.' from the output in any language environment. > >>>> > >>>> In the testsuite for std::system_category().message(int), we first switch the > >>>> thread UI language to en-US tomeet expectations in any language environment. > >>> Thanks, the patch looks good now. > >>> > >>>> libstdc++-v3/ChangeLog: > >>>> > >>>> * src/c++11/system_error.cc (system_error_category) [_WIN32]: > >>>> Fix typo in __MINGW32__ macro name. > >>>> Adjust behavior on the mingw32 target. > >>> The two lines above relate to the error_category/system_category.cc > >>> test, not the src/c++11/system_category.cc source file, right? > > > Sorry, I made a mistake. The correction for the __MINGW32__ macro > > is in error_category/system_category.cc. > > > >>>> Use FormatMessageA function instead of FormatMessage macro. > >>>> * testsuite/19_diagnostics/error_category/system_category.cc: > >>>> Adjust behavior on the mingw32 target. > >>> I've just noticed that there's no Signed-off-by trailer in the patch. > >>> Do you have a copyright assignment filed with the FSF? > >>> > >>> As documented at https://gcc.gnu.org/contribute.html#legal we need > >>> either a copyright assignment, or DCO sign-off. > >>> > >>> If you don't have a copyrigh assignment, could you please add the > >>> Signed-off-by trailer if you are willing and able to agree to the > >>> terms of https://gcc.gnu.org/dco.html ? > >> Actually, don't worry, I don't think this change can be considered > >> "legally significant", because the changes are small except for some > >> whitespace changes and typo fixes. > >> > >> So I'll push this to the git repo - thanks for fixing it! > > > Thank you for your reminder! > > > > Hmm, when I test this with mingw-w64 under Wine, I get: > > > > /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc > > 01ec:fixme:nls:RtlSetThreadPreferredUILanguages 8, 00000001400A8122, > > 000000000021FE8C > > /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc:127: > > void test03(): Assertion 'msg == "Access is denied"' > > failed. > > FAIL: /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc > > > > Inspecting the 'msg' string, the content is "Access denied", which is > > what we have on trunk today. > > > > Can you confirm whether it should be "Access is denied" (as in your > > patch) or "Access denied"? > > I apologize that I haven't tested it on Wine, but the the error message > I encountered on my Windows 11 Version 10.0.26200 system was "Access is > denied," which is the same as the one recorded in Microsoft Docs, see > here: > https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src/Debug/system-error-codes--0-499-.md > or > https://winprotocoldoc.z19.web.core.windows.net/MS-ERREF/%5bMS-ERREF%5d.pdf > This is probably because Wine is implemented differently than Microsoft > Windows. :( Thanks for checking. Somebody else has also confirmed that the strings are different for Windows and Wine. > > Perhaps we also need to add checks for the Wine environment, like those > found at > https://web.archive.org/web/20131028024505/https://www.winehq.org/pipermail/wine-devel/2008-September/069387.html Hmm, we could do. I think it's easier to just check that the string starts with "Access " and ends with " denied", which will match both strings. I'll change that.
On Sat, 10 Jan 2026 at 13:37, Jonathan Wakely <jwakely.gcc@gmail.com> wrote: > > On Sat, 10 Jan 2026 at 13:28, Wang Jinghao <zheng.xianyuwang@gmail.com> wrote: > > > > > > On 2026/1/10 20:50, Jonathan Wakely wrote: > > > On Sat, 10 Jan 2026 at 12:24, Jonathan Wakely <jwakely.gcc@gmail.com> wrote: > > >> On Sat, 10 Jan 2026 at 12:17, Jonathan Wakely <jwakely.gcc@gmail.com> wrote: > > >>> On Sat, 10 Jan 2026 at 10:45, Wang Jinghao <zheng.xianyuwang@gmail.com> wrote: > > >>>> On the mingw32 target, std::system_category().message(int) uses > > >>>> FormatMessage api to format error messages. When the error message contains > > >>>> insert sequences, it is unsafe not to use the FORMAT_MESSAGE_OGNORE_INSERTS > > >>>> flag, as seen at: > > >>>> https://devblogs.microsoft.com/oldnewthing/20071128-00/?p=24353 > > >>>> > > >>>> The output of FormatMessage ends with "\r\n" and includes a Full stop character > > >>>> used by the current thread's UI language. Now, we will remove "\r\n" and > > >>>> any trailing '.' from the output in any language environment. > > >>>> > > >>>> In the testsuite for std::system_category().message(int), we first switch the > > >>>> thread UI language to en-US tomeet expectations in any language environment. > > >>> Thanks, the patch looks good now. > > >>> > > >>>> libstdc++-v3/ChangeLog: > > >>>> > > >>>> * src/c++11/system_error.cc (system_error_category) [_WIN32]: > > >>>> Fix typo in __MINGW32__ macro name. > > >>>> Adjust behavior on the mingw32 target. > > >>> The two lines above relate to the error_category/system_category.cc > > >>> test, not the src/c++11/system_category.cc source file, right? > > > > > > Sorry, I made a mistake. The correction for the __MINGW32__ macro > > > > is in error_category/system_category.cc. > > > > > > >>>> Use FormatMessageA function instead of FormatMessage macro. > > >>>> * testsuite/19_diagnostics/error_category/system_category.cc: > > >>>> Adjust behavior on the mingw32 target. > > >>> I've just noticed that there's no Signed-off-by trailer in the patch. > > >>> Do you have a copyright assignment filed with the FSF? > > >>> > > >>> As documented at https://gcc.gnu.org/contribute.html#legal we need > > >>> either a copyright assignment, or DCO sign-off. > > >>> > > >>> If you don't have a copyrigh assignment, could you please add the > > >>> Signed-off-by trailer if you are willing and able to agree to the > > >>> terms of https://gcc.gnu.org/dco.html ? > > >> Actually, don't worry, I don't think this change can be considered > > >> "legally significant", because the changes are small except for some > > >> whitespace changes and typo fixes. > > >> > > >> So I'll push this to the git repo - thanks for fixing it! > > > > > > Thank you for your reminder! > > > > > > > Hmm, when I test this with mingw-w64 under Wine, I get: > > > > > > /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc > > > 01ec:fixme:nls:RtlSetThreadPreferredUILanguages 8, 00000001400A8122, > > > 000000000021FE8C > > > /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc:127: > > > void test03(): Assertion 'msg == "Access is denied"' > > > failed. > > > FAIL: /home/jwakely/src/gcc/gcc/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc > > > > > > Inspecting the 'msg' string, the content is "Access denied", which is > > > what we have on trunk today. > > > > > > Can you confirm whether it should be "Access is denied" (as in your > > > patch) or "Access denied"? > > > > I apologize that I haven't tested it on Wine, but the the error message > > I encountered on my Windows 11 Version 10.0.26200 system was "Access is > > denied," which is the same as the one recorded in Microsoft Docs, see > > here: > > https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src/Debug/system-error-codes--0-499-.md > > or > > https://winprotocoldoc.z19.web.core.windows.net/MS-ERREF/%5bMS-ERREF%5d.pdf > > This is probably because Wine is implemented differently than Microsoft > > Windows. :( > > Thanks for checking. Somebody else has also confirmed that the strings > are different for Windows and Wine. > > > > > Perhaps we also need to add checks for the Wine environment, like those > > found at > > https://web.archive.org/web/20131028024505/https://www.winehq.org/pipermail/wine-devel/2008-September/069387.html > > Hmm, we could do. I think it's easier to just check that the string > starts with "Access " and ends with " denied", which will match both > strings. I'll change that. I've pushed your patch to trunk now, and will backport it to the release branches at a later date. Thanks again for the fix.
On Sat, 10 Jan 2026 at 13:54, Jonathan Wakely <jwakely.gcc@gmail.com> wrote: > > On Sat, 10 Jan 2026 at 13:37, Jonathan Wakely <jwakely.gcc@gmail.com> wrote: > I've pushed your patch to trunk now, and will backport it to the > release branches at a later date. It has now been pushed to the gcc-14 and gcc-15 branches. > > Thanks again for the fix.
diff --git a/libstdc++-v3/src/c++11/system_error.cc b/libstdc++-v3/src/c++11/system_error.cc index 548c88fcfde..7e3a3a0c904 100644 --- a/libstdc++-v3/src/c++11/system_error.cc +++ b/libstdc++-v3/src/c++11/system_error.cc @@ -161,22 +161,23 @@ namespace #if defined(_WIN32) && !defined(__CYGWIN__) char* buf = nullptr; auto len - = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM - | FORMAT_MESSAGE_ALLOCATE_BUFFER, - nullptr, - i, - LANG_USER_DEFAULT, - reinterpret_cast<LPTSTR>(&buf), - 0, - nullptr); + = FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM + | FORMAT_MESSAGE_ALLOCATE_BUFFER + | FORMAT_MESSAGE_IGNORE_INSERTS, + nullptr, + i, + LANG_USER_DEFAULT, + reinterpret_cast<LPTSTR>(&buf), + 0, + nullptr); if (len > 0) { struct deleter { void operator()(void* p) const { ::LocalFree(p); } }; std::unique_ptr<char[], deleter> guard(buf); - if (len > 3 && !__builtin_memcmp(buf + len - 3, ".\r\n", 3)) [[likely]] - len -= 3; + if (len > 2 && !__builtin_memcmp (buf + len - 2, "\r\n", 2)) [[likely]] + len -= 2 + (buf[len - 3] == '.'); return string(buf, len); } return string("Unknown error code"); diff --git a/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc b/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc index 250d34f9bb5..03577234c43 100644 --- a/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc +++ b/libstdc++-v3/testsuite/19_diagnostics/error_category/system_category.cc @@ -21,6 +21,11 @@ #include <locale> #include <testsuite_hooks.h> +#if defined __MINGW32__ || defined __MINGW64__ +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +#endif + void test01() { @@ -34,7 +39,7 @@ test02() const std::error_category& cat = std::system_category(); std::error_condition cond; -#if defined __MING32__ || defined __MINGW64__ +#if defined __MINGW32__ || defined __MINGW64__ cond = cat.default_error_condition(8); // ERROR_NOT_ENOUGH_MEMORY VERIFY( cond.value() == ENOMEM ); VERIFY( cond.category() == std::generic_category() ); @@ -112,9 +117,16 @@ test03() // set "C" locale to get expected message auto loc = std::locale::global(std::locale::classic()); -#if defined __MING32__ || defined __MINGW64__ +#if defined __MINGW32__ || defined __MINGW64__ + // On Windows, set thread preferred UI languages to "en-US" + // to get expected message + ULONG num_langs = 1; + SetThreadPreferredUILanguages(MUI_LANGUAGE_NAME, L"en-US\0", &num_langs); + std::string msg = std::system_category().message(5); // ERROR_ACCESS_DENIED - VERIFY(msg == "Access denied"); + VERIFY(msg == "Access is denied"); + + SetThreadPreferredUILanguages(MUI_RESET_FILTERS, nullptr, nullptr); #else std::string msg = std::system_category().message(EBADF); VERIFY( msg.find("file") != std::string::npos );