]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
tool_doswin: fix to use curl socket functions
authorViktor Szakats <commit@vsz.me>
Sat, 20 Sep 2025 11:34:08 +0000 (13:34 +0200)
committerViktor Szakats <commit@vsz.me>
Mon, 22 Sep 2025 08:11:30 +0000 (10:11 +0200)
Replace `WSASocketW()` with `CURL_SOCKET()`. Also replace a call
to `socketclose()` with `sclose()`. According to a comment,
`socketclose()` was chosen to silence test 1498 (and 2300) reporting
`MEMORY FAILURE`. These reports were accurate, and were caused by
calling `WSASocketW()` instead of `socket()` (now `CURL_SOCKET()`).

This also fixes the curl `sclose()` call on an error branch, which is
now correctly paired with a curl socket open. The mismatched open/close
calls caused an issue in TrackMemory-enabled (aka `CURLDEBUG`) builds.

Docs confirm that `socket()` is defaulting to overlapped I/O, matching
the replaced `WSASocketW()` call:
https://learn.microsoft.com/windows/win32/api/winsock2/nf-winsock2-socket#remarks

Also:
- checksrc: ban `WSASocket*()` functions.
- report `SOCKERRNO` instead of `GetLastError()` for socket calls,
  to match the rest of the codebase.

Follow-up to 9a2663322c330ff11275abafd612e9c99407a94a #17572

Closes #18633

scripts/checksrc.pl
src/tool_doswin.c

index 0eeab72323f96448ddaa3a4c67304f204a1c63c9..0012ccdae1ad00254ccfdefb3783e6411a061c1a 100755 (executable)
@@ -74,6 +74,9 @@ my %banfunc = (
     "LoadLibraryEx" => 1,
     "LoadLibraryExA" => 1,
     "LoadLibraryExW" => 1,
+    "WSASocket" => 1,
+    "WSASocketA" => 1,
+    "WSASocketW" => 1,
     "_waccess" => 1,
     "_access" => 1,
     "access" => 1,
index 0450e5707ba2dad0d64ab7d6074dc46757604a53..29f8cecbd799838b42d136c522d6a822be0e6a3c 100644 (file)
@@ -769,14 +769,14 @@ static DWORD WINAPI win_stdin_thread_func(void *thread_data)
                                        &clientAddrLen);
 
   if(socket_w == CURL_SOCKET_BAD) {
-    errorf("accept error: %08lx", GetLastError());
+    errorf("accept error: %d", SOCKERRNO);
     goto ThreadCleanup;
   }
 
-  closesocket(tdata->socket_l); /* sclose here fails test 1498 */
+  sclose(tdata->socket_l);
   tdata->socket_l = CURL_SOCKET_BAD;
   if(shutdown(socket_w, SD_RECEIVE) == SOCKET_ERROR) {
-    errorf("shutdown error: %08lx", GetLastError());
+    errorf("shutdown error: %d", SOCKERRNO);
     goto ThreadCleanup;
   }
   for(;;) {
@@ -835,11 +835,9 @@ curl_socket_t win32_stdin_read_thread(void)
     }
     /* Create the listening socket for the thread. When it starts, it will
     * accept our connection and begin writing STDIN data to the connection. */
-    tdata->socket_l = WSASocketW(AF_INET, SOCK_STREAM,
-                                 IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED);
-
+    tdata->socket_l = CURL_SOCKET(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     if(tdata->socket_l == CURL_SOCKET_BAD) {
-      errorf("WSASocketW error: %08lx", GetLastError());
+      errorf("socket() error: %d", SOCKERRNO);
       break;
     }
 
@@ -850,20 +848,20 @@ curl_socket_t win32_stdin_read_thread(void)
     /* Bind to any available loopback port */
     result = bind(tdata->socket_l, (SOCKADDR*)&selfaddr, socksize);
     if(result == SOCKET_ERROR) {
-      errorf("bind error: %08lx", GetLastError());
+      errorf("bind error: %d", SOCKERRNO);
       break;
     }
 
     /* Bind to any available loopback port */
     result = getsockname(tdata->socket_l, (SOCKADDR*)&selfaddr, &socksize);
     if(result == SOCKET_ERROR) {
-      errorf("getsockname error: %08lx", GetLastError());
+      errorf("getsockname error: %d", SOCKERRNO);
       break;
     }
 
     result = listen(tdata->socket_l, 1);
     if(result == SOCKET_ERROR) {
-      errorf("listen error: %08lx", GetLastError());
+      errorf("listen error: %d", SOCKERRNO);
       break;
     }
 
@@ -891,7 +889,7 @@ curl_socket_t win32_stdin_read_thread(void)
     /* Connect to the thread and rearrange our own STDIN handles */
     socket_r = CURL_SOCKET(AF_INET, SOCK_STREAM, IPPROTO_TCP);
     if(socket_r == CURL_SOCKET_BAD) {
-      errorf("socket error: %08lx", GetLastError());
+      errorf("socket error: %d", SOCKERRNO);
       break;
     }
 
@@ -899,12 +897,12 @@ curl_socket_t win32_stdin_read_thread(void)
     setsockopt(socket_r, SOL_SOCKET, SO_DONTLINGER, 0, 0);
 
     if(connect(socket_r, (SOCKADDR*)&selfaddr, socksize) == SOCKET_ERROR) {
-      errorf("connect error: %08lx", GetLastError());
+      errorf("connect error: %d", SOCKERRNO);
       break;
     }
 
     if(shutdown(socket_r, SD_SEND) == SOCKET_ERROR) {
-      errorf("shutdown error: %08lx", GetLastError());
+      errorf("shutdown error: %d", SOCKERRNO);
       break;
     }