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})
endif()
target_link_libraries(${_target_name} ${LIB_SELECTED} ${CURL_NETWORK_AND_TIME_LIBS} ${_more_libs})
target_compile_definitions(${_target_name} PRIVATE "CURL_NO_OLDIES"
"$<$<BOOL:${WIN32}>:WIN32_LEAN_AND_MEAN>" "$<$<BOOL:${MSVC}>:_CRT_SECURE_NO_DEPRECATE>")
target_compile_definitions(${_target_name} PRIVATE "CURL_NO_OLDIES" "$<$<BOOL:${WIN32}>:WIN32_LEAN_AND_MEAN>")
set_target_properties(${_target_name} PROPERTIES OUTPUT_NAME "${_target}" PROJECT_LABEL "Example ${_target}" UNITY_BUILD OFF)
endforeach()

View File

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

View File

@ -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 */

View File

@ -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 <stdio.h>
#include <stdlib.h>

View File

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

View File

@ -26,6 +26,9 @@
* </DESC>
*/
#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

View File

@ -25,10 +25,17 @@
* Upload to a file:// URL
* </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>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <curl/curl.h>
#ifdef _WIN32
#undef stat

View File

@ -25,9 +25,16 @@
* FTP wildcard pattern matching
* </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 <curl/curl.h>
struct callback_data {
FILE *output;
};

View File

@ -21,14 +21,19 @@
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include <stdio.h>
#include <curl/curl.h>
/* <DESC>
* Get a single file from an FTP server.
* </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 {
const char *filename;

View File

@ -21,15 +21,20 @@
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
/* <DESC>
* Checks a single file's size and mtime from an FTP server.
* </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)
{

View File

@ -21,15 +21,21 @@
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include <stdio.h>
#include <curl/curl.h>
/* <DESC>
* Similar to ftpget.c but also stores the received response-lines
* in a separate file using our own callback!
* </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)
{
FILE *writehere = (FILE *)data;

View File

@ -21,15 +21,19 @@
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include <stdio.h>
#include <curl/curl.h>
/* <DESC>
* Get a single file from an FTPS server.
* </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 {
const char *filename;

View File

@ -21,14 +21,26 @@
* 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 <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <curl/curl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#ifdef _WIN32
#include <io.h>
#undef stat
@ -40,12 +52,6 @@
#include <unistd.h>
#endif
/* <DESC>
* Performs an FTP upload and renames the file just after a successful
* transfer.
* </DESC>
*/
#define LOCAL_FILE "/tmp/uploadthis.txt"
#define UPLOAD_FILE_AS "while-uploading.txt"
#define REMOTE_URL "ftp://example.com/" UPLOAD_FILE_AS

View File

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

View File

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

View File

@ -25,15 +25,17 @@
* Multiplexed HTTP/2 downloads over a single connection
* </DESC>
*/
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for _snprintf(), fopen(), strerror() */
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#if defined(_MSC_VER) && (_MSC_VER < 1900)
#define snprintf _snprintf
#endif
/* curl stuff */
#include <curl/curl.h>
@ -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;

View File

@ -25,6 +25,12 @@
* HTTP/2 server push
* </DESC>
*/
#ifdef _MSC_VER
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS /* for _snprintf(), fopen() */
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -32,14 +38,14 @@
/* curl stuff */
#include <curl/curl.h>
#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)

View File

@ -25,6 +25,13 @@
* Multiplexed HTTP/2 uploads over a single connection
* </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 <stdlib.h>
#include <string.h>
@ -38,6 +45,16 @@
#include <unistd.h>
#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
#undef stat
#define stat _stat
@ -50,16 +67,6 @@
#define snprintf _snprintf
#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
#define gettimeofday(a, b) my_gettimeofday((a), (b))
static int my_gettimeofday(struct timeval *tp, void *tzp)

View File

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

View File

@ -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 <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <curl/curl.h>
#ifdef _WIN32
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <windows.h>
#define strcasecmp _stricmp
#define strncasecmp _strnicmp

View File

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

View File

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

View File

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

View File

@ -25,6 +25,12 @@
* Shows HTTPS usage with client certs and optional ssl engine use.
* </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>

View File

@ -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 <stdio.h>

View File

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

View File

@ -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. */
#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_DEPRECATE
#define _CRT_SECURE_NO_DEPRECATE /* for fopen(), getenv(), etc. */
#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 */