build: update to not need _CRT_NONSTDC_NO_DEPRECATE with MSVC

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
This commit is contained in:
Viktor Szakats 2026-01-07 19:37:02 +01:00
parent dbc4603b09
commit e50aa46fb2
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
25 changed files with 54 additions and 49 deletions

View 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;
}

View 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);
}

View file

@ -89,10 +89,6 @@
#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
@ -808,6 +804,13 @@
# 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
/*

View 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

View 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

View 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;

View 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);

View 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;
}
}

View 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);