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
This commit is contained in:
Viktor Szakats 2025-10-21 11:51:02 +02:00
parent 1e1ec7f6c2
commit 5fa2d8320c
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
27 changed files with 201 additions and 63 deletions

View file

@ -83,8 +83,7 @@ foreach(_target IN LISTS COMPLICATED_MAY_BUILD check_PROGRAMS _all) # keep 'COM
add_dependencies(curl-examples ${_target_name}) add_dependencies(curl-examples ${_target_name})
endif() endif()
target_link_libraries(${_target_name} ${LIB_SELECTED} ${CURL_NETWORK_AND_TIME_LIBS} ${_more_libs}) target_link_libraries(${_target_name} ${LIB_SELECTED} ${CURL_NETWORK_AND_TIME_LIBS} ${_more_libs})
target_compile_definitions(${_target_name} PRIVATE "CURL_NO_OLDIES" target_compile_definitions(${_target_name} PRIVATE "CURL_NO_OLDIES" "$<$<BOOL:${WIN32}>:WIN32_LEAN_AND_MEAN>")
"$<$<BOOL:${WIN32}>:WIN32_LEAN_AND_MEAN>" "$<$<BOOL:${MSVC}>:_CRT_SECURE_NO_DEPRECATE>")
set_target_properties(${_target_name} PROPERTIES OUTPUT_NAME "${_target}" PROJECT_LABEL "Example ${_target}" UNITY_BUILD OFF) set_target_properties(${_target_name} PROPERTIES OUTPUT_NAME "${_target}" PROJECT_LABEL "Example ${_target}" UNITY_BUILD OFF)
endforeach() endforeach()

View file

@ -26,6 +26,12 @@
* one the server supports/wants. * one the server supports/wants.
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen() */
#endif
#endif
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>

View file

@ -34,13 +34,13 @@
int main(void) { printf("Platform not supported.\n"); return 1; } int main(void) { printf("Platform not supported.\n"); return 1; }
#else #else
#ifdef _WIN32 #ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _CRT_NONSTDC_NO_DEPRECATE #ifndef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE #define _CRT_NONSTDC_NO_DEPRECATE /* for strdup() */
#endif #endif
#endif
#ifdef _WIN32
#if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600 #if !defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600
#undef _WIN32_WINNT #undef _WIN32_WINNT
#define _WIN32_WINNT 0x0600 /* Requires Windows Vista */ #define _WIN32_WINNT 0x0600 /* Requires Windows Vista */

View file

@ -34,6 +34,11 @@
* dd if=/dev/urandom of=file_1M.bin bs=1M count=1 * 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 <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>

View file

@ -25,6 +25,11 @@
* Import and export cookies with COOKIELIST. * Import and export cookies with COOKIELIST.
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for _snprintf() */
#endif
#endif
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>

View file

@ -26,6 +26,9 @@
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER #ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for strerror() */
#endif
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS #ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS /* for inet_addr() */ #define _WINSOCK_DEPRECATED_NO_WARNINGS /* for inet_addr() */
#endif #endif

View file

@ -25,10 +25,17 @@
* Upload to a file:// URL * Upload to a file:// URL
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen() */
#endif
#endif
#include <stdio.h> #include <stdio.h>
#include <curl/curl.h>
#include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h>
#include <curl/curl.h>
#ifdef _WIN32 #ifdef _WIN32
#undef stat #undef stat

View file

@ -25,9 +25,16 @@
* FTP wildcard pattern matching * FTP wildcard pattern matching
* </DESC> * </DESC>
*/ */
#include <curl/curl.h> #ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen() */
#endif
#endif
#include <stdio.h> #include <stdio.h>
#include <curl/curl.h>
struct callback_data { struct callback_data {
FILE *output; FILE *output;
}; };

View file

@ -21,14 +21,19 @@
* SPDX-License-Identifier: curl * SPDX-License-Identifier: curl
* *
***************************************************************************/ ***************************************************************************/
#include <stdio.h>
#include <curl/curl.h>
/* <DESC> /* <DESC>
* Get a single file from an FTP server. * Get a single file from an FTP server.
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen() */
#endif
#endif
#include <stdio.h>
#include <curl/curl.h>
struct FtpFile { struct FtpFile {
const char *filename; const char *filename;

View file

@ -21,15 +21,20 @@
* SPDX-License-Identifier: curl * SPDX-License-Identifier: curl
* *
***************************************************************************/ ***************************************************************************/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
/* <DESC> /* <DESC>
* Checks a single file's size and mtime from an FTP server. * Checks a single file's size and mtime from an FTP server.
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen() */
#endif
#endif
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data) static size_t throw_away(void *ptr, size_t size, size_t nmemb, void *data)
{ {

View file

@ -21,15 +21,21 @@
* SPDX-License-Identifier: curl * SPDX-License-Identifier: curl
* *
***************************************************************************/ ***************************************************************************/
#include <stdio.h>
#include <curl/curl.h>
/* <DESC> /* <DESC>
* Similar to ftpget.c but also stores the received response-lines * Similar to ftpget.c but also stores the received response-lines
* in a separate file using our own callback! * in a separate file using our own callback!
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen() */
#endif
#endif
#include <stdio.h>
#include <curl/curl.h>
static size_t write_response(void *ptr, size_t size, size_t nmemb, void *data) static size_t write_response(void *ptr, size_t size, size_t nmemb, void *data)
{ {
FILE *writehere = (FILE *)data; FILE *writehere = (FILE *)data;

View file

@ -21,15 +21,19 @@
* SPDX-License-Identifier: curl * SPDX-License-Identifier: curl
* *
***************************************************************************/ ***************************************************************************/
#include <stdio.h>
#include <curl/curl.h>
/* <DESC> /* <DESC>
* Get a single file from an FTPS server. * Get a single file from an FTPS server.
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen() */
#endif
#endif
#include <stdio.h>
#include <curl/curl.h>
struct FtpFile { struct FtpFile {
const char *filename; const char *filename;

View file

@ -21,14 +21,26 @@
* SPDX-License-Identifier: curl * SPDX-License-Identifier: curl
* *
***************************************************************************/ ***************************************************************************/
/* <DESC>
* Performs an FTP upload and renames the file just after a successful
* transfer.
* </DESC>
*/
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen(), strerror() */
#endif
#endif
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <curl/curl.h> #include <curl/curl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#ifdef _WIN32 #ifdef _WIN32
#include <io.h> #include <io.h>
#undef stat #undef stat
@ -40,12 +52,6 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
/* <DESC>
* Performs an FTP upload and renames the file just after a successful
* transfer.
* </DESC>
*/
#define LOCAL_FILE "/tmp/uploadthis.txt" #define LOCAL_FILE "/tmp/uploadthis.txt"
#define UPLOAD_FILE_AS "while-uploading.txt" #define UPLOAD_FILE_AS "while-uploading.txt"
#define REMOTE_URL "ftp://example.com/" UPLOAD_FILE_AS #define REMOTE_URL "ftp://example.com/" UPLOAD_FILE_AS

View file

@ -25,9 +25,15 @@
* Upload to FTP, resuming failed transfers. Active mode. * Upload to FTP, resuming failed transfers. Active mode.
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen(), sscanf() */
#endif
#endif
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <curl/curl.h> #include <curl/curl.h>
/* parse headers for Content-Length */ /* parse headers for Content-Length */

View file

@ -25,8 +25,15 @@
* Preload domains to HSTS * Preload domains to HSTS
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for strcpy() */
#endif
#endif
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <curl/curl.h> #include <curl/curl.h>
struct entry { struct entry {

View file

@ -25,15 +25,17 @@
* Multiplexed HTTP/2 downloads over a single connection * Multiplexed HTTP/2 downloads over a single connection
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for _snprintf(), fopen(), strerror() */
#endif
#endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
/* curl stuff */ /* curl stuff */
#include <curl/curl.h> #include <curl/curl.h>
@ -44,6 +46,10 @@
#define CURLPIPE_MULTIPLEX 0L #define CURLPIPE_MULTIPLEX 0L
#endif #endif
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
struct transfer { struct transfer {
FILE *out; FILE *out;
CURL *curl; CURL *curl;

View file

@ -25,6 +25,12 @@
* HTTP/2 server push * HTTP/2 server push
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for _snprintf(), fopen() */
#endif
#endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -32,14 +38,14 @@
/* curl stuff */ /* curl stuff */
#include <curl/curl.h> #include <curl/curl.h>
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
#ifndef CURLPIPE_MULTIPLEX #ifndef CURLPIPE_MULTIPLEX
#error "too old libcurl, cannot do HTTP/2 server push!" #error "too old libcurl, cannot do HTTP/2 server push!"
#endif #endif
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
static FILE *out_download; static FILE *out_download;
static void dump(const char *text, unsigned char *ptr, size_t size, char nohex) static void dump(const char *text, unsigned char *ptr, size_t size, char nohex)

View file

@ -25,6 +25,13 @@
* Multiplexed HTTP/2 uploads over a single connection * Multiplexed HTTP/2 uploads over a single connection
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for '_snprintf(), fopen(), localtime(),
strerror() */
#endif
#endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -38,6 +45,16 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
/* curl stuff */
#include <curl/curl.h>
#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 #ifdef _WIN32
#undef stat #undef stat
#define stat _stat #define stat _stat
@ -50,16 +67,6 @@
#define snprintf _snprintf #define snprintf _snprintf
#endif #endif
/* curl stuff */
#include <curl/curl.h>
#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 #ifdef _MSC_VER
#define gettimeofday(a, b) my_gettimeofday((a), (b)) #define gettimeofday(a, b) my_gettimeofday((a), (b))
static int my_gettimeofday(struct timeval *tp, void *tzp) static int my_gettimeofday(struct timeval *tp, void *tzp)

View file

@ -25,6 +25,12 @@
* HTTP PUT with easy interface and read callback * HTTP PUT with easy interface and read callback
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen() */
#endif
#endif
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>

View file

@ -31,18 +31,21 @@
* The transfer's log is written to disk only if the transfer fails. * 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 <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
#include <string.h> #include <string.h>
#include <curl/curl.h> #include <curl/curl.h>
#ifdef _WIN32 #ifdef _WIN32
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <windows.h> #include <windows.h>
#define strcasecmp _stricmp #define strcasecmp _stricmp
#define strncasecmp _strnicmp #define strncasecmp _strnicmp

View file

@ -25,6 +25,12 @@
* Simple HTTP GET that stores the headers in a separate file * Simple HTTP GET that stores the headers in a separate file
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen() */
#endif
#endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>

View file

@ -25,6 +25,11 @@
* Gets a file using an SFTP URL. * Gets a file using an SFTP URL.
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen() */
#endif
#endif
#include <stdio.h> #include <stdio.h>

View file

@ -25,9 +25,15 @@
* Upload to SFTP, resuming a previously aborted transfer. * Upload to SFTP, resuming a previously aborted transfer.
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen() */
#endif
#endif
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <curl/curl.h> #include <curl/curl.h>
/* read data to upload */ /* read data to upload */

View file

@ -25,6 +25,12 @@
* Shows HTTPS usage with client certs and optional ssl engine use. * Shows HTTPS usage with client certs and optional ssl engine use.
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen() */
#endif
#endif
#include <stdio.h> #include <stdio.h>
#include <curl/curl.h> #include <curl/curl.h>

View file

@ -56,6 +56,12 @@
* This software synchronises your computer clock only when you issue * This software synchronises your computer clock only when you issue
* it with --synctime. By default, it only display the webserver's clock. * 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 <stdio.h> #include <stdio.h>

View file

@ -25,6 +25,12 @@
* Download a given URL into a local file named page.out. * Download a given URL into a local file named page.out.
* </DESC> * </DESC>
*/ */
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for fopen() */
#endif
#endif
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>

View file

@ -90,11 +90,15 @@
/* Disable Visual Studio warnings: 4127 "conditional expression is constant" */ /* Disable Visual Studio warnings: 4127 "conditional expression is constant" */
#pragma warning(disable:4127) #pragma warning(disable:4127)
/* Avoid VS2005 and upper complaining about portable C functions. */ /* Avoid VS2005 and upper complaining about portable C functions. */
#ifndef _CRT_NONSTDC_NO_DEPRECATE #ifndef _CRT_NONSTDC_NO_DEPRECATE /* mingw-w64 v2+. MS SDK ~10+/~VS2017+. */
#define _CRT_NONSTDC_NO_DEPRECATE /* for strdup(), write(), etc. */ #define _CRT_NONSTDC_NO_DEPRECATE /* for close(), fileno(), strdup(),
unlink(), etc. */
#endif #endif
#ifndef _CRT_SECURE_NO_DEPRECATE #ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE /* for fopen(), getenv(), etc. */ #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
#endif /* _MSC_VER */ #endif /* _MSC_VER */