From: Viktor Szakats Date: Tue, 21 Oct 2025 09:51:02 +0000 (+0200) Subject: build: tidy-up MSVC CRT warning suppression macros X-Git-Tag: rc-8_18_0-1~250 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5fa2d8320c4196435c1d554b06dfdcca73824dec;p=thirdparty%2Fcurl.git build: tidy-up MSVC CRT warning suppression macros - curl_setup.h: replace `_CRT_SECURE_NO_DEPRECATE` with `_CRT_SECURE_NO_WARNINGS`, which seems to be the preferred, more recent macro for this. Also syncing with libssh2. They are equivalent for curl sources with the supported compilers. - cmake: stop setting `_CRT_SECURE_NO_DEPRECATE` globally for examples. - examples: suppress CRT deprecation warnings on a per-file basis. To make it work when compiling examples out of curl's build systems. Use `_CRT_SECURE_NO_WARNINGS`. - examples: document the functions requiring `_CRT_SECURE_NO_WARNINGS`. - examples/block_ip: delete superfluous `_CRT_SECURE_NO_WARNINGS`. - examples/block_ip: limit `_CRT_NONSTDC_NO_DEPRECATE` to MSVC. - examples/log_failed_transfers: fix to set `_CRT_SECURE_NO_WARNINGS` before headers and limit to MSVC. - curl_setup.h: document which SDKs support `_CRT_NONSTDC_NO_DEPRECATE`. Closes #19175 --- diff --git a/docs/examples/CMakeLists.txt b/docs/examples/CMakeLists.txt index c86e8439fd..31f2808981 100644 --- a/docs/examples/CMakeLists.txt +++ b/docs/examples/CMakeLists.txt @@ -83,8 +83,7 @@ foreach(_target IN LISTS COMPLICATED_MAY_BUILD check_PROGRAMS _all) # keep 'COM add_dependencies(curl-examples ${_target_name}) endif() target_link_libraries(${_target_name} ${LIB_SELECTED} ${CURL_NETWORK_AND_TIME_LIBS} ${_more_libs}) - target_compile_definitions(${_target_name} PRIVATE "CURL_NO_OLDIES" - "$<$:WIN32_LEAN_AND_MEAN>" "$<$:_CRT_SECURE_NO_DEPRECATE>") + target_compile_definitions(${_target_name} PRIVATE "CURL_NO_OLDIES" "$<$:WIN32_LEAN_AND_MEAN>") set_target_properties(${_target_name} PROPERTIES OUTPUT_NAME "${_target}" PROJECT_LABEL "Example ${_target}" UNITY_BUILD OFF) endforeach() diff --git a/docs/examples/anyauthput.c b/docs/examples/anyauthput.c index 12d8de76c9..eb5959ca22 100644 --- a/docs/examples/anyauthput.c +++ b/docs/examples/anyauthput.c @@ -26,6 +26,12 @@ * one the server supports/wants. * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen() */ +#endif +#endif + #include #include #include diff --git a/docs/examples/block_ip.c b/docs/examples/block_ip.c index 290f92a61e..ef7056dc43 100644 --- a/docs/examples/block_ip.c +++ b/docs/examples/block_ip.c @@ -34,13 +34,13 @@ int main(void) { printf("Platform not supported.\n"); return 1; } #else -#ifdef _WIN32 -#ifndef _CRT_SECURE_NO_WARNINGS -#define _CRT_SECURE_NO_WARNINGS -#endif +#ifdef _MSC_VER #ifndef _CRT_NONSTDC_NO_DEPRECATE -#define _CRT_NONSTDC_NO_DEPRECATE +#define _CRT_NONSTDC_NO_DEPRECATE /* for strdup() */ +#endif #endif + +#ifdef _WIN32 #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600 #undef _WIN32_WINNT #define _WIN32_WINNT 0x0600 /* Requires Windows Vista */ diff --git a/docs/examples/chkspeed.c b/docs/examples/chkspeed.c index ea71e6ad70..962180a560 100644 --- a/docs/examples/chkspeed.c +++ b/docs/examples/chkspeed.c @@ -34,6 +34,11 @@ * dd if=/dev/urandom of=file_1M.bin bs=1M count=1 * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for ctime() */ +#endif +#endif #include #include diff --git a/docs/examples/cookie_interface.c b/docs/examples/cookie_interface.c index 4eee1a605d..a031380f12 100644 --- a/docs/examples/cookie_interface.c +++ b/docs/examples/cookie_interface.c @@ -25,6 +25,11 @@ * Import and export cookies with COOKIELIST. * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for _snprintf() */ +#endif +#endif #include #include diff --git a/docs/examples/externalsocket.c b/docs/examples/externalsocket.c index 6cab781a46..11fedb07a2 100644 --- a/docs/examples/externalsocket.c +++ b/docs/examples/externalsocket.c @@ -26,6 +26,9 @@ * */ #ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for strerror() */ +#endif #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS #define _WINSOCK_DEPRECATED_NO_WARNINGS /* for inet_addr() */ #endif diff --git a/docs/examples/fileupload.c b/docs/examples/fileupload.c index fa9de2aa4f..da69e4a8ea 100644 --- a/docs/examples/fileupload.c +++ b/docs/examples/fileupload.c @@ -25,10 +25,17 @@ * Upload to a file:// URL * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen() */ +#endif +#endif + #include -#include -#include #include +#include + +#include #ifdef _WIN32 #undef stat diff --git a/docs/examples/ftp-wildcard.c b/docs/examples/ftp-wildcard.c index 79386f9834..3bd2f69083 100644 --- a/docs/examples/ftp-wildcard.c +++ b/docs/examples/ftp-wildcard.c @@ -25,9 +25,16 @@ * FTP wildcard pattern matching * */ -#include +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen() */ +#endif +#endif + #include +#include + struct callback_data { FILE *output; }; diff --git a/docs/examples/ftpget.c b/docs/examples/ftpget.c index 391324bf64..f57974de1f 100644 --- a/docs/examples/ftpget.c +++ b/docs/examples/ftpget.c @@ -21,14 +21,19 @@ * SPDX-License-Identifier: curl * ***************************************************************************/ -#include - -#include - /* * Get a single file from an FTP server. * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen() */ +#endif +#endif + +#include + +#include struct FtpFile { const char *filename; diff --git a/docs/examples/ftpgetinfo.c b/docs/examples/ftpgetinfo.c index 549d3003a8..105bc60855 100644 --- a/docs/examples/ftpgetinfo.c +++ b/docs/examples/ftpgetinfo.c @@ -21,15 +21,20 @@ * SPDX-License-Identifier: curl * ***************************************************************************/ -#include -#include - -#include - /* * Checks a single file's size and mtime from an FTP server. * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen() */ +#endif +#endif + +#include +#include + +#include static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data) { diff --git a/docs/examples/ftpgetresp.c b/docs/examples/ftpgetresp.c index 6dce4ab749..eec727bf94 100644 --- a/docs/examples/ftpgetresp.c +++ b/docs/examples/ftpgetresp.c @@ -21,15 +21,21 @@ * SPDX-License-Identifier: curl * ***************************************************************************/ -#include - -#include - /* * Similar to ftpget.c but also stores the received response-lines * in a separate file using our own callback! * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen() */ +#endif +#endif + +#include + +#include + static size_t write_response(void *ptr, size_t size, size_t nmemb, void *data) { FILE *writehere = (FILE *)data; diff --git a/docs/examples/ftpsget.c b/docs/examples/ftpsget.c index f39046885e..22c94b8e93 100644 --- a/docs/examples/ftpsget.c +++ b/docs/examples/ftpsget.c @@ -21,15 +21,19 @@ * SPDX-License-Identifier: curl * ***************************************************************************/ - -#include - -#include - /* * Get a single file from an FTPS server. * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen() */ +#endif +#endif + +#include + +#include struct FtpFile { const char *filename; diff --git a/docs/examples/ftpupload.c b/docs/examples/ftpupload.c index b334ad3273..0a241db21c 100644 --- a/docs/examples/ftpupload.c +++ b/docs/examples/ftpupload.c @@ -21,14 +21,26 @@ * SPDX-License-Identifier: curl * ***************************************************************************/ +/* + * Performs an FTP upload and renames the file just after a successful + * transfer. + * + */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen(), strerror() */ +#endif +#endif + #include #include +#include +#include +#include +#include #include -#include -#include -#include -#include + #ifdef _WIN32 #include #undef stat @@ -40,12 +52,6 @@ #include #endif -/* - * Performs an FTP upload and renames the file just after a successful - * transfer. - * - */ - #define LOCAL_FILE "/tmp/uploadthis.txt" #define UPLOAD_FILE_AS "while-uploading.txt" #define REMOTE_URL "ftp://example.com/" UPLOAD_FILE_AS diff --git a/docs/examples/ftpuploadresume.c b/docs/examples/ftpuploadresume.c index b86c23ab86..a9af3f12cd 100644 --- a/docs/examples/ftpuploadresume.c +++ b/docs/examples/ftpuploadresume.c @@ -25,9 +25,15 @@ * Upload to FTP, resuming failed transfers. Active mode. * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen(), sscanf() */ +#endif +#endif #include #include + #include /* parse headers for Content-Length */ diff --git a/docs/examples/hsts-preload.c b/docs/examples/hsts-preload.c index a775ec9778..ad71c62cad 100644 --- a/docs/examples/hsts-preload.c +++ b/docs/examples/hsts-preload.c @@ -25,8 +25,15 @@ * Preload domains to HSTS * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for strcpy() */ +#endif +#endif + #include #include + #include struct entry { diff --git a/docs/examples/http2-download.c b/docs/examples/http2-download.c index e58742e59c..2caeaba512 100644 --- a/docs/examples/http2-download.c +++ b/docs/examples/http2-download.c @@ -25,15 +25,17 @@ * Multiplexed HTTP/2 downloads over a single connection * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for _snprintf(), fopen(), strerror() */ +#endif +#endif + #include #include #include #include -#if defined(_MSC_VER) && (_MSC_VER < 1900) -#define snprintf _snprintf -#endif - /* curl stuff */ #include @@ -44,6 +46,10 @@ #define CURLPIPE_MULTIPLEX 0L #endif +#if defined(_MSC_VER) && (_MSC_VER < 1900) +#define snprintf _snprintf +#endif + struct transfer { FILE *out; CURL *curl; diff --git a/docs/examples/http2-serverpush.c b/docs/examples/http2-serverpush.c index 8c43075b93..2664158a1f 100644 --- a/docs/examples/http2-serverpush.c +++ b/docs/examples/http2-serverpush.c @@ -25,6 +25,12 @@ * HTTP/2 server push * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for _snprintf(), fopen() */ +#endif +#endif + #include #include #include @@ -32,14 +38,14 @@ /* curl stuff */ #include -#if defined(_MSC_VER) && (_MSC_VER < 1900) -#define snprintf _snprintf -#endif - #ifndef CURLPIPE_MULTIPLEX #error "too old libcurl, cannot do HTTP/2 server push!" #endif +#if defined(_MSC_VER) && (_MSC_VER < 1900) +#define snprintf _snprintf +#endif + static FILE *out_download; static void dump(const char *text, unsigned char *ptr, size_t size, char nohex) diff --git a/docs/examples/http2-upload.c b/docs/examples/http2-upload.c index 0365d93bc5..e546ded162 100644 --- a/docs/examples/http2-upload.c +++ b/docs/examples/http2-upload.c @@ -25,6 +25,13 @@ * Multiplexed HTTP/2 uploads over a single connection * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for '_snprintf(), fopen(), localtime(), + strerror() */ +#endif +#endif + #include #include #include @@ -38,6 +45,16 @@ #include #endif +/* curl stuff */ +#include + +#ifndef CURLPIPE_MULTIPLEX +/* This little trick makes sure that we do not enable pipelining for libcurls + old enough to not have this symbol. It is _not_ defined to zero in a recent + libcurl header. */ +#define CURLPIPE_MULTIPLEX 0L +#endif + #ifdef _WIN32 #undef stat #define stat _stat @@ -50,16 +67,6 @@ #define snprintf _snprintf #endif -/* curl stuff */ -#include - -#ifndef CURLPIPE_MULTIPLEX -/* This little trick makes sure that we do not enable pipelining for libcurls - old enough to not have this symbol. It is _not_ defined to zero in a recent - libcurl header. */ -#define CURLPIPE_MULTIPLEX 0L -#endif - #ifdef _MSC_VER #define gettimeofday(a, b) my_gettimeofday((a), (b)) static int my_gettimeofday(struct timeval *tp, void *tzp) diff --git a/docs/examples/httpput.c b/docs/examples/httpput.c index a7fac75b4a..38fb641f52 100644 --- a/docs/examples/httpput.c +++ b/docs/examples/httpput.c @@ -25,6 +25,12 @@ * HTTP PUT with easy interface and read callback * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen() */ +#endif +#endif + #include #include #include diff --git a/docs/examples/log_failed_transfers.c b/docs/examples/log_failed_transfers.c index 23208d232c..a0d209ffc3 100644 --- a/docs/examples/log_failed_transfers.c +++ b/docs/examples/log_failed_transfers.c @@ -31,18 +31,21 @@ * The transfer's log is written to disk only if the transfer fails. * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen(), strerror(), vsnprintf() */ +#endif +#endif #include #include #include #include #include + #include #ifdef _WIN32 -#ifndef _CRT_SECURE_NO_WARNINGS -#define _CRT_SECURE_NO_WARNINGS -#endif #include #define strcasecmp _stricmp #define strncasecmp _strnicmp diff --git a/docs/examples/sepheaders.c b/docs/examples/sepheaders.c index 85938dc223..613bc26a3c 100644 --- a/docs/examples/sepheaders.c +++ b/docs/examples/sepheaders.c @@ -25,6 +25,12 @@ * Simple HTTP GET that stores the headers in a separate file * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen() */ +#endif +#endif + #include #include diff --git a/docs/examples/sftpget.c b/docs/examples/sftpget.c index 5eaf3416e8..287d5b253e 100644 --- a/docs/examples/sftpget.c +++ b/docs/examples/sftpget.c @@ -25,6 +25,11 @@ * Gets a file using an SFTP URL. * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen() */ +#endif +#endif #include diff --git a/docs/examples/sftpuploadresume.c b/docs/examples/sftpuploadresume.c index 72ae41382c..84bb425549 100644 --- a/docs/examples/sftpuploadresume.c +++ b/docs/examples/sftpuploadresume.c @@ -25,9 +25,15 @@ * Upload to SFTP, resuming a previously aborted transfer. * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen() */ +#endif +#endif #include #include + #include /* read data to upload */ diff --git a/docs/examples/simplessl.c b/docs/examples/simplessl.c index cf5477f945..a41a9ac29e 100644 --- a/docs/examples/simplessl.c +++ b/docs/examples/simplessl.c @@ -25,6 +25,12 @@ * Shows HTTPS usage with client certs and optional ssl engine use. * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen() */ +#endif +#endif + #include #include diff --git a/docs/examples/synctime.c b/docs/examples/synctime.c index 43fe5d312e..a9a746c30a 100644 --- a/docs/examples/synctime.c +++ b/docs/examples/synctime.c @@ -56,6 +56,12 @@ * This software synchronises your computer clock only when you issue * it with --synctime. By default, it only display the webserver's clock. */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for _snprintf(), fopen(), gmtime(), + localtime(), sscanf() */ +#endif +#endif #include diff --git a/docs/examples/url2file.c b/docs/examples/url2file.c index a0523f62db..b4cbe1c7eb 100644 --- a/docs/examples/url2file.c +++ b/docs/examples/url2file.c @@ -25,6 +25,12 @@ * Download a given URL into a local file named page.out. * */ +#ifdef _MSC_VER +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for fopen() */ +#endif +#endif + #include #include diff --git a/lib/curl_setup.h b/lib/curl_setup.h index bf7030f693..15688aab2c 100644 --- a/lib/curl_setup.h +++ b/lib/curl_setup.h @@ -90,11 +90,15 @@ /* 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 -#define _CRT_NONSTDC_NO_DEPRECATE /* for strdup(), write(), etc. */ -#endif -#ifndef _CRT_SECURE_NO_DEPRECATE -#define _CRT_SECURE_NO_DEPRECATE /* for fopen(), getenv(), etc. */ +#ifndef _CRT_NONSTDC_NO_DEPRECATE /* mingw-w64 v2+. MS SDK ~10+/~VS2017+. */ +#define _CRT_NONSTDC_NO_DEPRECATE /* for close(), fileno(), strdup(), + unlink(), etc. */ +#endif +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS /* for __sys_errlist, __sys_nerr, _wfopen(), + _wopen(), freopen(), getenv(), gmtime(), + sprintf(), strcpy(), wcscpy(), wcsncpy() + in tests: localtime(), open(), sscanf() */ #endif #endif /* _MSC_VER */