]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
build: update to not need `_CRT_NONSTDC_NO_DEPRECATE` with MSVC
authorViktor Szakats <commit@vsz.me>
Wed, 7 Jan 2026 18:37:02 +0000 (19:37 +0100)
committerViktor Szakats <commit@vsz.me>
Tue, 20 Jan 2026 22:19:54 +0000 (23:19 +0100)
Use non-deprecated CRT function variants on Windows.

- introduce `curlx_fdopen()`, `curlx_close()` and use them. Map them to
  non-deprecated, underscored, CRT functions on Windows.

- replace `close()` uses with either `sclose()` (for sockets) or
  `curlx_close()` (for files).

- map `fileno`, `unlink`, `isatty` to their non-deprecated, underscored,
  versions on Windows.

- tool_dirhie: map `mkdir` to `_mkdir` on Windows.

- easy: use `_strdup()` on Windows, regardless of how `HAVE_STRDUP` is
  set.

- cmake: assume `HAVE_STRDUP` on Windows. To allow dropping a detection
  hack using `_CRT_NONSTDC_NO_DEPRECATE` with MSVC. Windows always has
  `_strdup()` which the code uses, but also needs `HAVE_STRDUP` defined
  to disable curl's own `strdup()` implementation.

- curl_setup.h: drop `_CRT_NONSTDC_NO_DEPRECATE` as no longer necessary.

Closes #20212

25 files changed:
CMake/win32-cache.cmake
CMakeLists.txt
docs/examples/.checksrc
docs/internals/CODE_STYLE.md
lib/cf-socket.c
lib/curl_fopen.c
lib/curl_setup.h
lib/curlx/fopen.h
lib/easy.c
lib/file.c
lib/memdebug.c
lib/socketpair.c
lib/vquic/curl_ngtcp2.c
scripts/checksrc.pl
src/tool_cb_wrt.c
src/tool_dirhie.c
src/tool_findfile.c
src/tool_getpass.c
src/tool_operate.c
tests/libtest/lib518.c
tests/libtest/lib537.c
tests/libtest/lib568.c
tests/libtest/lib572.c
tests/server/tftpd.c
tests/server/util.c

index 3ddbfce7d3853cdc79df6953f91c87874c8558db..c4ee6fd1b9b471aab9a264974b31d41ac29217e0 100644 (file)
@@ -154,7 +154,6 @@ set(HAVE_SIGSETJMP 0)
 set(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID 1)
 set(HAVE_SOCKET 1)
 set(HAVE_SOCKETPAIR 0)
-set(HAVE_STRDUP 1)
 set(HAVE_STRERROR_R 0)
 set(HAVE_STROPTS_H 0)
 set(HAVE_STRUCT_SOCKADDR_STORAGE 1)
index 8103f98aec4155aa23798c0c4eb12875f299326b..20cd74542b4c491c4fb64052ad20c711f8bf2254 100644 (file)
@@ -193,9 +193,6 @@ if(WIN32)
 
   # Apply to all feature checks
   list(APPEND CMAKE_REQUIRED_DEFINITIONS "-DWIN32_LEAN_AND_MEAN")
-  if(MSVC)
-    list(APPEND CMAKE_REQUIRED_DEFINITIONS "-D_CRT_NONSTDC_NO_DEPRECATE")  # for strdup() detection
-  endif()
 
   set(CURL_TARGET_WINDOWS_VERSION "" CACHE STRING "Minimum target Windows version as hex string")
   if(CURL_TARGET_WINDOWS_VERSION)
@@ -1537,7 +1534,6 @@ check_symbol_exists("send"            "${CURL_INCLUDES}" HAVE_SEND)  # proto/bsd
 check_function_exists("sendmsg"       HAVE_SENDMSG)
 check_function_exists("sendmmsg"      HAVE_SENDMMSG)
 check_symbol_exists("select"          "${CURL_INCLUDES}" HAVE_SELECT)  # proto/bsdsocket.h sys/select.h sys/socket.h
-check_symbol_exists("strdup"          "string.h" HAVE_STRDUP)
 check_symbol_exists("memrchr"         "string.h" HAVE_MEMRCHR)
 check_symbol_exists("alarm"           "unistd.h" HAVE_ALARM)
 check_symbol_exists("fcntl"           "fcntl.h" HAVE_FCNTL)
@@ -1575,11 +1571,14 @@ check_function_exists("getrlimit"     HAVE_GETRLIMIT)
 check_function_exists("setlocale"     HAVE_SETLOCALE)
 check_function_exists("setrlimit"     HAVE_SETRLIMIT)
 
-if(NOT WIN32)
+if(WIN32)
+  set(HAVE_STRDUP 1)  # to not define local implementation. curl uses _strdup() on Windows.
+else()
   check_function_exists("if_nametoindex"  HAVE_IF_NAMETOINDEX)  # net/if.h
   check_function_exists("realpath"        HAVE_REALPATH)
   check_function_exists("sched_yield"     HAVE_SCHED_YIELD)
   check_symbol_exists("strcasecmp"        "string.h" HAVE_STRCASECMP)
+  check_symbol_exists("strdup"            "string.h" HAVE_STRDUP)
   check_symbol_exists("stricmp"           "string.h" HAVE_STRICMP)
   check_symbol_exists("strcmpi"           "string.h" HAVE_STRCMPI)
 endif()
index 8c7b0c901e80c9ed1086028209ef057aa2b38d7e..a9c6b4ed503ff86dd7ba8abaf7c5303fa7e2b2cc 100644 (file)
@@ -5,6 +5,7 @@
 allowfunc atoi
 allowfunc atol
 allowfunc calloc
+allowfunc close
 allowfunc fclose
 allowfunc fdopen
 allowfunc fopen
index 8be6592df5b866cb63c47fcd094ad0dd5d82c499..6167eaad001f6c1dd88a6110bb7b267536d638b1 100644 (file)
@@ -353,6 +353,7 @@ This is the full list of functions generally banned.
     atoi
     atol
     calloc
+    close
     CreateFile
     CreateFileA
     CreateFileW
index ebad57f371d0c39d510610eafb2c68453a67d6ed..8aa211367bfad15adcc2256594d3ff800d287e45 100644 (file)
@@ -349,7 +349,7 @@ static CURLcode socket_open(struct Curl_easy *data,
   if(fcntl(*sockfd, F_SETFD, FD_CLOEXEC) < 0) {
     failf(data, "fcntl set CLOEXEC: %s",
           curlx_strerror(SOCKERRNO, errbuf, sizeof(errbuf)));
-    close(*sockfd);
+    sclose(*sockfd);
     *sockfd = CURL_SOCKET_BAD;
     return CURLE_COULDNT_CONNECT;
   }
index 9559c0d0c4f5e7b3a82905a50d64839cbba21e2c..3d5ee5002dbc11652c7b040c2ece43784de374f8 100644 (file)
@@ -147,7 +147,7 @@ CURLcode Curl_fopen(struct Curl_easy *data, const char *filename,
 
 fail:
   if(fd != -1) {
-    close(fd);
+    curlx_close(fd);
     unlink(tempstore);
   }
 
index 750ba90d2d4c8f1a1d914b466659eb398ccee2f2..178694c66edfdb4d3d4e2e93ad9b26f468eddc5e 100644 (file)
 #ifdef _MSC_VER
 /* Disable Visual Studio warnings: 4127 "conditional expression is constant" */
 #pragma warning(disable:4127)
-/* Avoid VS2005 and upper complaining about portable C functions. */
-#ifndef _CRT_NONSTDC_NO_DEPRECATE  /* mingw-w64 v2+. MS SDK ~10+/~VS2017+. */
-#define _CRT_NONSTDC_NO_DEPRECATE  /* for close(), fileno(), unlink(), etc. */
-#endif
 #ifndef _CRT_SECURE_NO_WARNINGS
 #define _CRT_SECURE_NO_WARNINGS  /* for getenv(), tests: sscanf() */
 #endif
 #  define read(fd, buf, count)  (ssize_t)_read(fd, buf, curlx_uztoui(count))
 #  undef  write
 #  define write(fd, buf, count) (ssize_t)_write(fd, buf, curlx_uztoui(count))
+/* Avoid VS2005+ _CRT_NONSTDC_NO_DEPRECATE warnings about non-portable funcs */
+#  undef fileno
+#  define fileno(fh) _fileno(fh)
+#  undef unlink
+#  define unlink(fn) _unlink(fn)
+#  undef isatty
+#  define isatty(fd) _isatty(fd)
 #endif
 
 /*
index a395590d8e0fd7d3a1e06a4434e96c4d2cfde7ea..e78a474b975c0219dd2db61738a6f9ff477879b3 100644 (file)
@@ -50,14 +50,18 @@ int curlx_win32_open(const char *filename, int oflag, ...);
 int curlx_win32_rename(const char *oldpath, const char *newpath);
 #define CURLX_FOPEN_LOW(fname, mode)       curlx_win32_fopen(fname, mode)
 #define CURLX_FREOPEN_LOW(fname, mode, fh) curlx_win32_freopen(fname, mode, fh)
+#define CURLX_FDOPEN_LOW                   _fdopen
 #define curlx_stat(fname, stp)             curlx_win32_stat(fname, stp)
 #define curlx_open                         curlx_win32_open
+#define curlx_close                        _close
 #define curlx_rename                       curlx_win32_rename
 #else
 #define CURLX_FOPEN_LOW                    fopen
 #define CURLX_FREOPEN_LOW                  freopen
+#define CURLX_FDOPEN_LOW                   fdopen
 #define curlx_stat(fname, stp)             stat(fname, stp)
 #define curlx_open                         open
+#define curlx_close                        close
 #define curlx_rename                       rename
 #endif
 
@@ -71,7 +75,7 @@ int curlx_win32_rename(const char *oldpath, const char *newpath);
 #else
 #define curlx_fopen             CURLX_FOPEN_LOW
 #define curlx_freopen           CURLX_FREOPEN_LOW
-#define curlx_fdopen            fdopen
+#define curlx_fdopen            CURLX_FDOPEN_LOW
 #define curlx_fclose            fclose
 #endif
 
index 51936660cee2cefa5906510bfbea5e2dd365c152..cc505b5b3f822f72ca93d8d3e012d45344d01e99 100644 (file)
@@ -99,12 +99,10 @@ static curl_simple_lock s_lock = CURL_SIMPLE_LOCK_INIT;
  * ways, but at this point it must be defined as the system-supplied strdup
  * so the callback pointer is initialized correctly.
  */
-#ifdef HAVE_STRDUP
 #ifdef _WIN32
 #define system_strdup _strdup
-#else
+#elif defined(HAVE_STRDUP)
 #define system_strdup strdup
-#endif
 #else
 #define system_strdup Curl_strdup
 #endif
index 753aed2dbe30430473f6aafa8c401d5e889e2c4f..d4cfedff1743a3f532d09236c32392e05bba7740 100644 (file)
@@ -87,7 +87,7 @@ static void file_cleanup(struct FILEPROTO *file)
   Curl_safefree(file->freepath);
   file->path = NULL;
   if(file->fd != -1) {
-    close(file->fd);
+    curlx_close(file->fd);
     file->fd = -1;
   }
 }
@@ -312,7 +312,7 @@ static CURLcode file_upload(struct Curl_easy *data,
   /* treat the negative resume offset value as the case of "-" */
   if(data->state.resume_from < 0) {
     if(fstat(fd, &file_stat)) {
-      close(fd);
+      curlx_close(fd);
       failf(data, "cannot get the size of %s", file->path);
       return CURLE_WRITE_ERROR;
     }
@@ -367,7 +367,7 @@ static CURLcode file_upload(struct Curl_easy *data,
     result = Curl_pgrsUpdate(data);
 
 out:
-  close(fd);
+  curlx_close(fd);
   Curl_multi_xfer_ulbuf_release(data, xfer_ulbuf);
 
   return result;
index 2851bcefa9c6e63ffa7a44695ae7dc2752bf255f..457034ebdae2227ed453e777681031b6de84b77f 100644 (file)
@@ -497,8 +497,7 @@ ALLOC_FUNC
 FILE *curl_dbg_fdopen(int filedes, const char *mode,
                       int line, const char *source)
 {
-  /* !checksrc! disable BANNEDFUNC 1 */
-  FILE *res = fdopen(filedes, mode);
+  FILE *res = CURLX_FDOPEN_LOW(filedes, mode);
   if(source)
     curl_dbg_log("FILE %s:%d fdopen(\"%d\",\"%s\") = %p\n",
                  source, line, filedes, mode, (void *)res);
index ec45926237e8611f334caf0f4c801f29335e89c4..cefbe8523866e23037c7c0d44ce1815a932707c7 100644 (file)
@@ -64,8 +64,8 @@ static int wakeup_pipe(curl_socket_t socks[2], bool nonblocking)
 #ifdef HAVE_FCNTL
   if(fcntl(socks[0], F_SETFD, FD_CLOEXEC) ||
      fcntl(socks[1], F_SETFD, FD_CLOEXEC)) {
-    close(socks[0]);
-    close(socks[1]);
+    sclose(socks[0]);
+    sclose(socks[1]);
     socks[0] = socks[1] = CURL_SOCKET_BAD;
     return -1;
   }
@@ -73,8 +73,8 @@ static int wakeup_pipe(curl_socket_t socks[2], bool nonblocking)
   if(nonblocking) {
     if(curlx_nonblock(socks[0], TRUE) < 0 ||
        curlx_nonblock(socks[1], TRUE) < 0) {
-      close(socks[0]);
-      close(socks[1]);
+      sclose(socks[0]);
+      sclose(socks[1]);
       socks[0] = socks[1] = CURL_SOCKET_BAD;
       return -1;
     }
@@ -107,8 +107,8 @@ static int wakeup_socketpair(curl_socket_t socks[2], bool nonblocking)
   if(nonblocking) {
     if(curlx_nonblock(socks[0], TRUE) < 0 ||
        curlx_nonblock(socks[1], TRUE) < 0) {
-      close(socks[0]);
-      close(socks[1]);
+      sclose(socks[0]);
+      sclose(socks[1]);
       return -1;
     }
   }
index 57a1543be3504ec48bfa79fe2f913a085b12d395..f624c02fa3335591132cd20e2ee58808751163ff 100644 (file)
@@ -55,6 +55,7 @@
 #include "../cf-socket.h"
 #include "../connect.h"
 #include "../progress.h"
+#include "../curlx/fopen.h"
 #include "../curlx/dynbuf.h"
 #include "../http1.h"
 #include "../select.h"
@@ -449,7 +450,7 @@ static void qlog_callback(void *user_data, uint32_t flags,
     ssize_t rc = write(ctx->qlogfd, data, datalen);
     if(rc == -1) {
       /* on write error, stop further write attempts */
-      close(ctx->qlogfd);
+      curlx_close(ctx->qlogfd);
       ctx->qlogfd = -1;
     }
   }
@@ -2140,7 +2141,7 @@ static void cf_ngtcp2_ctx_close(struct cf_ngtcp2_ctx *ctx)
   if(!ctx->initialized)
     return;
   if(ctx->qlogfd != -1) {
-    close(ctx->qlogfd);
+    curlx_close(ctx->qlogfd);
   }
   ctx->qlogfd = -1;
   Curl_vquic_tls_cleanup(&ctx->tls);
index d065b0bc064b386ae51aac630c9f92ecd444f81c..2fd505b495a9b92fd1ffcb0b67691be68cd27b5c 100755 (executable)
@@ -70,6 +70,7 @@ my %banfunc = (
     "atoi" => 1,
     "atol" => 1,
     "calloc" => 1,
+    "close" => 1,
     "CreateFile" => 1,
     "CreateFileA" => 1,
     "CreateFileW" => 1,
index 17c7d80b0f3d99b5b53ae949f23edf6f366abcd1..cc005844af068d4ba35c90e9bbf41a86a6c4afcc 100644 (file)
@@ -89,7 +89,7 @@ bool tool_create_output_file(struct OutStruct *outs,
     if(fd != -1) {
       file = curlx_fdopen(fd, "wb");
       if(!file)
-        close(fd);
+        curlx_close(fd);
     }
   }
 
index 945d8796089d8d0f86e8bfeaa6d1262e07707e2c..3f5c987e954720558f329777eff747e66777d5b6 100644 (file)
  ***************************************************************************/
 #include "tool_setup.h"
 
-#ifdef _WIN32
-#include <direct.h>
-#endif
-
 #include "tool_dirhie.h"
 #include "tool_msgs.h"
 
-#if defined(_WIN32) || (defined(MSDOS) && !defined(__DJGPP__))
+#ifdef _WIN32
+#  include <direct.h>
+#  define mkdir(x, y) _mkdir(x)
+#elif defined(MSDOS) && !defined(__DJGPP__)
 #  define mkdir(x, y) (mkdir)(x)
 #endif
 
index 1fcd74f5a194f68add301f4c73309dede38b8946..2745a2eb3ccf7363ecfeacfbae93c1c368dc155f 100644 (file)
@@ -74,7 +74,7 @@ static char *checkhome(const char *home, const char *fname, bool dotscore)
       int fd = curlx_open(c, O_RDONLY);
       if(fd >= 0) {
         char *path = curlx_strdup(c);
-        close(fd);
+        curlx_close(fd);
         curl_free(c);
         return path;
       }
index a8c7cb2439667dc9d1b0cd890dd4b2d99f0e1626..99ded8f4c5b57b40fa83aa65eb0c786dc53700fe 100644 (file)
@@ -187,7 +187,7 @@ char *getpass_r(const char *prompt, /* prompt to display */
   }
 
   if(STDIN_FILENO != fd)
-    close(fd);
+    curlx_close(fd);
 
   return password; /* return pointer to buffer */
 }
index 13ebe83feea79bce9af4f1560815ec96d396e604..1cddf47b00bb47a845103a96c08bfb5ba11dbcaa 100644 (file)
@@ -289,7 +289,7 @@ static CURLcode pre_transfer(struct per_transfer *per)
     {
       helpf("cannot open '%s'", per->uploadfile);
       if(per->infd != -1) {
-        close(per->infd);
+        curlx_close(per->infd);
         per->infd = STDIN_FILENO;
       }
       return CURLE_READ_ERROR;
@@ -621,7 +621,7 @@ static CURLcode post_per_transfer(struct per_transfer *per,
   else
 #endif
     if(per->infdopen)
-      close(per->infd);
+      curlx_close(per->infd);
 
   if(per->skip)
     goto skip;
index 8431ed87d7347937f504266ac286376ee912fe3a..d405c6dfa058d576bc787a6eec2a93936448e2e7 100644 (file)
@@ -63,7 +63,7 @@ static void t518_close_file_descriptors(void)
       t518_num_open.rlim_cur < t518_num_open.rlim_max;
       t518_num_open.rlim_cur++)
     if(t518_testfd[t518_num_open.rlim_cur] > 0)
-      close(t518_testfd[t518_num_open.rlim_cur]);
+      curlx_close(t518_testfd[t518_num_open.rlim_cur]);
   curlx_free(t518_testfd);
   t518_testfd = NULL;
 }
@@ -333,7 +333,7 @@ static int t518_test_rlimit(int keep_open)
       for(t518_num_open.rlim_cur = 0;
           t518_testfd[t518_num_open.rlim_cur] >= 0;
           t518_num_open.rlim_cur++)
-        close(t518_testfd[t518_num_open.rlim_cur]);
+        curlx_close(t518_testfd[t518_num_open.rlim_cur]);
       curlx_free(t518_testfd);
       t518_testfd = NULL;
       curlx_free(memchunk);
index 436a9eb0a49bca906829b12923899012be120f62..1bf6fcba067802b0d2b36a3a6d2744774a7fb5f8 100644 (file)
@@ -60,7 +60,7 @@ static void t537_close_file_descriptors(void)
       t537_num_open.rlim_cur < t537_num_open.rlim_max;
       t537_num_open.rlim_cur++)
     if(t537_testfd[t537_num_open.rlim_cur] > 0)
-      close(t537_testfd[t537_num_open.rlim_cur]);
+      curlx_close(t537_testfd[t537_num_open.rlim_cur]);
   curlx_free(t537_testfd);
   t537_testfd = NULL;
 }
@@ -334,7 +334,7 @@ static int t537_test_rlimit(int keep_open)
       for(t537_num_open.rlim_cur = t537_num_open.rlim_max;
           t537_testfd[t537_num_open.rlim_cur] >= 0;
           t537_num_open.rlim_cur++) {
-        close(t537_testfd[t537_num_open.rlim_cur]);
+        curlx_close(t537_testfd[t537_num_open.rlim_cur]);
         t537_testfd[t537_num_open.rlim_cur] = -1;
       }
 
index 6106ed8598b9bd3f3e05aa8014a7bd4ee2bde058..8c1e5dd7fa29608a5cf0635b009f2adab9120def 100644 (file)
@@ -75,7 +75,7 @@ static CURLcode test_lib568(const char *URL)
     goto test_cleanup;
   }
   fstat(sdp, &file_info);
-  close(sdp);
+  curlx_close(sdp);
 
   sdpf = curlx_fopen(libtest_arg2, "rb");
   if(!sdpf) {
index d1cb80479225f162adabc759fa3619b2e0d52aff..38edd77ecea54a809881f153b67ef4e12cc80c4a 100644 (file)
@@ -93,7 +93,7 @@ static CURLcode test_lib572(const char *URL)
     goto test_cleanup;
   }
   fstat(params, &file_info);
-  close(params);
+  curlx_close(params);
 
   paramsf = curlx_fopen(libtest_arg2, "rb");
   if(!paramsf) {
index e1b3a8b936fa5c1cd68ee934849ea32afd106e7f..1e0fcc9a48d793547ce691742f4323eca6b095d7 100644 (file)
@@ -849,7 +849,7 @@ send_ack:
   write_behind(test, pf->f_convert);
   /* close the output file as early as possible after upload completion */
   if(test->ofile > 0) {
-    close(test->ofile);
+    curlx_close(test->ofile);
     test->ofile = 0;
   }
 
@@ -875,7 +875,7 @@ send_ack:
 abort:
   /* make sure the output file is closed in case of abort */
   if(test->ofile > 0) {
-    close(test->ofile);
+    curlx_close(test->ofile);
     test->ofile = 0;
   }
   return;
@@ -1305,7 +1305,7 @@ static int test_tftpd(int argc, char **argv)
 tftpd_cleanup:
 
   if(test.ofile > 0)
-    close(test.ofile);
+    curlx_close(test.ofile);
 
   if((peer != sock) && (peer != CURL_SOCKET_BAD))
     sclose(peer);
index da959f3ed992fec980b654144eb4c4abf899d1b4..1cf62c0e762cbdb220252286bbfc8d991f275774 100644 (file)
@@ -379,7 +379,7 @@ static void exit_signal_handler(int signum)
 #endif
       static const char msg[] = "exit_signal_handler: called\n";
       (void)!write(fd, msg, sizeof(msg) - 1);
-      close(fd);
+      curlx_close(fd);
     }
     else {
       static const char msg[] = "exit_signal_handler: failed opening ";