Skip to content

tests/server: fix to check against winsock2 error codes on Windows - #16612

Closed
vszakats wants to merge 7 commits into
curl:masterfrom
vszakats:sockerr-vs-errno-t
Closed

vszakats wants to merge 7 commits into
curl:masterfrom
vszakats:sockerr-vs-errno-t

Conversation

@vszakats

@vszakats vszakats commented Mar 7, 2025 •

Copy link
Copy Markdown
Member

Windows's winsock2 returns socket errors via WSAGetLastError() and
not via errno like most systems out there. This was covered by
switching to the SOCKERRNO curl macro earlier. But, on Windows the
returned socket error codes have different values than the standard
POSIX errno values. Existing code was using the POSIX values for all
these checks. Meaning they never actually matched on Windows.

This patch defines a set of SOCKERRNO constants that map to the
correct socket error values for Windows and other platforms.

The reverse issue exists in core curl code, which redefines POSIX errno
values to winsock2 ones, breaking non-socket uses on Windows.

Cherry-picked from #15000
Follow-up to adcfd4f #16553
Bug: #16553 (comment)


As for the naming: This uses SOCK prefix which seemed like the best
quick idea. The cleanest would be a CURL_SOCK prefix, then CURL_S,
or we may just use S, like SEINTR.

@vszakats vszakats added tests Windows Windows-specific labels Mar 7, 2025
@vszakats
vszakats force-pushed the sockerr-vs-errno-t branch from 9a77d5b to 8c0709a Compare March 7, 2025 13:30
@vszakats vszakats mentioned this pull request Mar 7, 2025
17 tasks done
@vszakats

vszakats commented Mar 7, 2025 •

Copy link
Copy Markdown
Member Author

Alternate suggestions for the prefix? I'm proposing SOCK as in
SOCKEINTR. Greppable, but not too long.

vszakats added 4 commits March 7, 2025 18:02
Windows's winsock2 returns socket errors via `WSAGetLastError()` and
not via `errno` like most systems out there. This was covered by
switching to the `SOCKERRNO` curl macro earlier. But, on Windows the
returned socket error codes have different values than the standard
POSIX errno values. Existing code was using the POSIX values for all
these checks. Meaning they never actually matched on Windows.

This patch defines a set of `SOCKERRNO` constants that map to the
correct socket error values for Windows and other platforms.

The same issue exists in core curl code, which redefines the POSIX
errno values to winsock2 ones, breaking non-socket uses on Windows.

Cherry-picked from curl#15000
Follow-up to adcfd4f curl#16553
Bug: curl#16553 (comment)
Drop the mapping and check for EAGAIN as-is. This will always fail on
Windows. These checks should always also check for the alternate
error that's also supported on Windows: SOCKEWOULDBLOCK.

All existing checks do that. (They are in sws.c)
@vszakats
vszakats force-pushed the sockerr-vs-errno-t branch from 3b3d45e to e84d7ae Compare March 7, 2025 17:03
@vszakats

vszakats commented Mar 7, 2025 •

Copy link
Copy Markdown
Member Author

One more surprise: sockfilt redefines select() on Windows to a local implementation, but sets the non-socket errno in error cases :( Question if it sets its errors as the stock select() would do in non-error cases. The local implementation adds file handle support. Those operations possibly don't result in WSA error codes, so would need translation?

edit: It's consistently setting errno for file operations. This is an issue on Windows, where select() must normally return a WSA code via SOCKERRNO. Does it mean that in sockfilt, specifically for file operations these patches are breaking stuff, while fixing winsock error handling? Maybe in sockfile we should be checking for both SOCKERRNO and errno? It's called from a single place.

edit2: The select() local implementation returns GetLastError() via errno, but those values don't map to POSIX values either, suggesting that the error checking never really worked here.

Assuming error handling is critical at all here. If it worked for such a long time, maybe it isn't.

https://en.wikipedia.org/wiki/Errno.h
https://learn.microsoft.com/windows/win32/debug/system-error-codes--0-499-
https://learn.microsoft.com/windows/win32/winsock/windows-sockets-error-codes-2

https://learn.microsoft.com/windows/win32/winsock/error-codes-errno-h-errno-and-wsagetlasterror-2

a662f86

@testclutch

This comment was marked as outdated.

@vszakats vszakats closed this in abf80aa Mar 8, 2025
vszakats added a commit that referenced this pull request Mar 8, 2025
These were not used in curl sources at all.

Except `EDQUOT` which was used after `mkdir()` in `src/tool_dirhie.c`
for error display. It should not be redefined to a winsock2 error.
This makes the "exceeded your quota" error correctly appear on Windows,
if detected, after operations that create directories.

After this patch there remain 14 `E*` macro redefines on Windows,
down from 40 before this patch.

Bug: #16553 (comment)
Ref: #16612
Ref: #16605
Closes #16615
@vszakats
vszakats deleted the sockerr-vs-errno-t branch March 8, 2025 00:54
vszakats added a commit that referenced this pull request Mar 12, 2025
…ixes

Before this patch, standard `E*` errno codes were redefined on Windows,
onto matching winsock2 `WSA*` error codes, which have different values.
This broke uses where using the `E*` value in non-socket context, or
other places expecting a POSIX `errno`, e.g. file I/O, threads, IDN or
interfacing with dependencies.

Fix it by introducing a curl-specific `SOCKE*` set of macros that map to
`WSA*` on Windows and standard POSIX codes on other platforms. Then
verify and update the code to use `SOCKE*` or `E*` macro depending on
context.

- Add `SOCKE*` macros that map to either winsock2 or POSIX error codes.
  And use them with `SOCKERRNO` or in contexts requiring
  platform-dependent socket error codes.

  This fixes `E*` uses which were supposed be POSIX values, not `WSA*`
  socket errors, on Windows:
  - lib/curl_multibyte.c
  - lib/curl_threads.c
  - lib/idn.c
  - lib/vtls/gtls.c
  - lib/vtls/rustls.c
  - src/tool_cb_wrt.c
  - src/tool_dirhie.c

- Ban `E*` codes having a `SOCKE*` mapping, via checksrc.
  Authored-by: Daniel Stenberg

- Add exceptions for `E*` codes used in file I/O, or other contexts
  requiring POSIX error codes.

Also:
- ftp: fix missing `SOCKEACCES` mapping for Windows.
- add `SOCKENOMEM` for `Curl_getaddrinfo()` via `asyn-thread.c`.
- tests/server/sockfilt: fix to set `SOCKERRNO` in local `select()`
  override on Windows.
- lib/inet_ntop: fix to return `WSAEINVAL` on Windows, where `ENOSPC` is
  used on other platforms. To simulate Windows' built-in `inet_ntop()`,
  as tested on a Win10 machine.
  Note:
  - WINE returns `STATUS_INVALID_PARAMETER` = `0xC000000D`.
  - Microsoft documentation says it returns `WSA_INVALID_PARAMETER`
    (= `ERROR_INVALID_PARAMETER`) 87:
    https://learn.microsoft.com/windows/win32/api/ws2tcpip/nf-ws2tcpip-inet_ntop#return-value
- lib/inet_ntop: drop redundant `CURL_SETERRNO(ENOSPC)`.
  `inet_ntop4()` already sets it before returning `NULL`.
- replace stray `WSAEWOULDBLOCK` with `USE_WINSOCK` macro to detect
  winsock2.
- move existing `SOCKE*` mappings from `tests/server` to
  `curl_setup_once.h`.
- add missing `EINTR`, `EINVAL` constants for WinCE.

Follow-up to abf80aa #16612
Follow-up to d69425e #16615
Bug: #16553 (comment)

Closes #16621
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
Windows's winsock2 returns socket errors via `WSAGetLastError()` and
not via `errno` like most systems out there. This was covered by
switching to the `SOCKERRNO` curl macro earlier. But, on Windows the
returned socket error codes have different values than the standard
POSIX errno values. Existing code was using the POSIX values for all
these checks. Meaning they never actually matched on Windows.

This patch defines a set of `SOCKERRNO` constants that map to the
correct socket error values for Windows and other platforms.

The reverse issue exists in core curl code, which redefines POSIX errno
values to winsock2 ones, breaking non-socket uses on Windows.

Cherry-picked from curl#15000
Follow-up to adcfd4f curl#16553
Bug: curl#16553 (comment)

Closes curl#16612
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
These were not used in curl sources at all.

Except `EDQUOT` which was used after `mkdir()` in `src/tool_dirhie.c`
for error display. It should not be redefined to a winsock2 error.
This makes the "exceeded your quota" error correctly appear on Windows,
if detected, after operations that create directories.

After this patch there remain 14 `E*` macro redefines on Windows,
down from 40 before this patch.

Bug: curl#16553 (comment)
Ref: curl#16612
Ref: curl#16605
Closes curl#16615
pps83 pushed a commit to pps83/curl that referenced this pull request Apr 26, 2025
…ixes

Before this patch, standard `E*` errno codes were redefined on Windows,
onto matching winsock2 `WSA*` error codes, which have different values.
This broke uses where using the `E*` value in non-socket context, or
other places expecting a POSIX `errno`, e.g. file I/O, threads, IDN or
interfacing with dependencies.

Fix it by introducing a curl-specific `SOCKE*` set of macros that map to
`WSA*` on Windows and standard POSIX codes on other platforms. Then
verify and update the code to use `SOCKE*` or `E*` macro depending on
context.

- Add `SOCKE*` macros that map to either winsock2 or POSIX error codes.
  And use them with `SOCKERRNO` or in contexts requiring
  platform-dependent socket error codes.

  This fixes `E*` uses which were supposed be POSIX values, not `WSA*`
  socket errors, on Windows:
  - lib/curl_multibyte.c
  - lib/curl_threads.c
  - lib/idn.c
  - lib/vtls/gtls.c
  - lib/vtls/rustls.c
  - src/tool_cb_wrt.c
  - src/tool_dirhie.c

- Ban `E*` codes having a `SOCKE*` mapping, via checksrc.
  Authored-by: Daniel Stenberg

- Add exceptions for `E*` codes used in file I/O, or other contexts
  requiring POSIX error codes.

Also:
- ftp: fix missing `SOCKEACCES` mapping for Windows.
- add `SOCKENOMEM` for `Curl_getaddrinfo()` via `asyn-thread.c`.
- tests/server/sockfilt: fix to set `SOCKERRNO` in local `select()`
  override on Windows.
- lib/inet_ntop: fix to return `WSAEINVAL` on Windows, where `ENOSPC` is
  used on other platforms. To simulate Windows' built-in `inet_ntop()`,
  as tested on a Win10 machine.
  Note:
  - WINE returns `STATUS_INVALID_PARAMETER` = `0xC000000D`.
  - Microsoft documentation says it returns `WSA_INVALID_PARAMETER`
    (= `ERROR_INVALID_PARAMETER`) 87:
    https://learn.microsoft.com/windows/win32/api/ws2tcpip/nf-ws2tcpip-inet_ntop#return-value
- lib/inet_ntop: drop redundant `CURL_SETERRNO(ENOSPC)`.
  `inet_ntop4()` already sets it before returning `NULL`.
- replace stray `WSAEWOULDBLOCK` with `USE_WINSOCK` macro to detect
  winsock2.
- move existing `SOCKE*` mappings from `tests/server` to
  `curl_setup_once.h`.
- add missing `EINTR`, `EINVAL` constants for WinCE.

Follow-up to abf80aa curl#16612
Follow-up to d69425e curl#16615
Bug: curl#16553 (comment)

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

Labels

tests Windows Windows-specific

Development

Successfully merging this pull request may close these issues.

2 participants