`<system_error>`: `system_category()` maps `WSAECONNREFUSED` etc. onto `errc`, but not their `winerror.h` equivalents (`ERROR_CONNECTION_REFUSED`, `ERROR_NETNAME_DELETED`, ...)

Aperta Adatta ai principianti
#6,471 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
75/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp

Direzione di ricerca

Inizia da stl/src/syserror.cpp e ispeziona le voci di _Win_errtab che mappano i codici Winsock alle condizioni errc portabili. Aggiungi la copertura per i sette codici winerror.h elencati, quindi esegui i controlli di repro.cpp o i test pertinenti del sistema degli errori STL e verifica che tutti e 13 i confronti restituiscano true.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement

Describe the bug

system_category().default_error_condition() maps the Winsock network error codes onto the portable errc
conditions, but not the winerror.h codes that Windows reports for the same failures. So whether
ec == errc::connection_refused holds for a refused connection depends on which API reported it.

The winerror.h codes are what overlapped I/O completions report: GetOverlappedResult /
GetQueuedCompletionStatus translate the NTSTATUS with the usual NTSTATUS-to-Win32 mapping, so a
ConnectEx to a closed port completes with ERROR_CONNECTION_REFUSED (not WSAECONNREFUSED), and a
reset connection completes a WSARecv with ERROR_NETNAME_DELETED (not WSAECONNRESET).

Missing from _Win_errtab (checked against main, stl/src/syserror.cpp):

winerror.h code Winsock equivalent, already mapped condition
ERROR_CONNECTION_REFUSED (1225) WSAECONNREFUSED connection_refused
ERROR_PORT_UNREACHABLE (1234) WSAECONNREFUSED connection_refused
ERROR_NETWORK_UNREACHABLE (1231) WSAENETUNREACH network_unreachable
ERROR_HOST_UNREACHABLE (1232) WSAEHOSTUNREACH host_unreachable
ERROR_CONNECTION_ABORTED (1236) WSAECONNABORTED connection_aborted
ERROR_NETNAME_DELETED (64) WSAECONNRESET connection_reset
ERROR_CANCELLED (1223) (ERROR_OPERATION_ABORTED) operation_canceled

Command-line test case

C:\Temp>type repro.cpp
#include <winsock2.h>
#include <windows.h>

#include <cstdio>
#include <system_error>
using namespace std;

void check(const char* name, int ev, const char* condName, errc cond) {
    printf("%-25s (%5d) == errc::%-19s : %s\n", name, ev, condName,
        error_code{ev, system_category()} == cond ? "true" : "false");
}
#define CHECK(ev, cond) check(#ev, ev, #cond, errc::cond)

int main() {
    CHECK(WSAECONNREFUSED, connection_refused);
    CHECK(ERROR_CONNECTION_REFUSED, connection_refused);
    CHECK(ERROR_PORT_UNREACHABLE, connection_refused);
    CHECK(WSAENETUNREACH, network_unreachable);
    CHECK(ERROR_NETWORK_UNREACHABLE, network_unreachable);
    CHECK(WSAEHOSTUNREACH, host_unreachable);
    CHECK(ERROR_HOST_UNREACHABLE, host_unreachable);
    CHECK(WSAECONNABORTED, connection_aborted);
    CHECK(ERROR_CONNECTION_ABORTED, connection_aborted);
    CHECK(WSAECONNRESET, connection_reset);
    CHECK(ERROR_NETNAME_DELETED, connection_reset);
    CHECK(ERROR_OPERATION_ABORTED, operation_canceled);
    CHECK(ERROR_CANCELLED, operation_canceled);
}

C:\Temp>cl /EHsc /W4 /WX /std:c++latest repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.51.36260 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

/std:c++latest is provided as a preview of language features from the latest C++
working draft, and we're eager to hear about bugs and suggestions for improvements.
However, note that these features are provided as-is without support, and subject
to changes or removal as the working draft evolves. See
https://go.microsoft.com/fwlink/?linkid=2045807 for details.

repro.cpp
Microsoft (R) Incremental Linker Version 14.51.36260.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:repro.exe
repro.obj

C:\Temp>repro.exe
WSAECONNREFUSED           (10061) == errc::connection_refused  : true
ERROR_CONNECTION_REFUSED  ( 1225) == errc::connection_refused  : false
ERROR_PORT_UNREACHABLE    ( 1234) == errc::connection_refused  : false
WSAENETUNREACH            (10051) == errc::network_unreachable : true
ERROR_NETWORK_UNREACHABLE ( 1231) == errc::network_unreachable : false
WSAEHOSTUNREACH           (10065) == errc::host_unreachable    : true
ERROR_HOST_UNREACHABLE    ( 1232) == errc::host_unreachable    : false
WSAECONNABORTED           (10053) == errc::connection_aborted  : true
ERROR_CONNECTION_ABORTED  ( 1236) == errc::connection_aborted  : false
WSAECONNRESET             (10054) == errc::connection_reset    : true
ERROR_NETNAME_DELETED     (   64) == errc::connection_reset    : false
ERROR_OPERATION_ABORTED   (  995) == errc::operation_canceled  : true
ERROR_CANCELLED           ( 1223) == errc::operation_canceled  : false

Expected behavior

Each winerror.h code compares equal to the same errc condition as its Winsock equivalent - all 13 lines true.

STL version

Microsoft Visual Studio Community 2026
Version 18.10.3

Also reproduced with VS 2022 17.14 (MSVC 14.44.35207). stl/src/syserror.cpp on main has none of these
seven codes.

Additional context

  • Boost.Asio works around exactly this, translating these codes by hand before reporting them
    (socket_ops.ipp):
    complete_iocp_connect maps ERROR_CONNECTION_REFUSED, ERROR_NETWORK_UNREACHABLE and ERROR_HOST_UNREACHABLE;
    complete_iocp_recv and recv map ERROR_NETNAME_DELETED -> connection_reset (and recv,
    ERROR_PORT_UNREACHABLE -> connection_refused). (complete_iocp_accept maps ERROR_NETNAME_DELETED to
    connection_aborted instead - for a connection reset before it was accepted - but connection_reset is what it
    means on an established connection, and what WSAECONNRESET already maps to.)
  • The same kind of gap as #2572 (WAIT_TIMEOUT vs errc::timed_out), fixed by #2744.
  • We hit this in Stroika, and now map these codes ourselves:
    https://github.com/SophistSolutions/Stroika/issues/1192
Lingua principale
C++
Stelle
11.2k
Fork
1.7k
Merge medio
4g 4h
PR unite (30g)
14

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di microsoft/STL

Tutte le issue di microsoft/STL

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.