]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
printf: fix mingw-w64 format checks
authorViktor Szakats <commit@vsz.me>
Thu, 22 Aug 2024 08:54:42 +0000 (10:54 +0200)
committerViktor Szakats <commit@vsz.me>
Mon, 2 Sep 2024 19:03:01 +0000 (21:03 +0200)
Change mingw-w64 printf format checks in public curl headers to use
`__MINGW_PRINTF_FORMAT` instead of `gnu_printf`. This syncs the format
checker with format string macros published via `curl/system.h`. (Also
disable format checks for mingw-w64 older than 3.0.0 (2013-09-20) and
classic-mingw, which do not support this macro.)

This fixes bogus format checker `-Wformat` warnings in 3rd party code
using curl format strings with the curl printf functions, when using
mingw-w64 7.0.0 (2019-11-10) and older (with GCC, MSVCRT).

It also allows to delete two workaounds for this within curl itself:
- setting `-D__USE_MINGW_ANSI_STDIO=1` for mingw-w64 via cmake and
  configure for `docs/examples` and `tests/http/clients`.
  Ref: c730c8549b5b67e7668ca5d2cd82c3cc183e125d #14640

The format check macro is incompatible (depending on mingw-w64 version
and configuration) with the C99 `%z` (`size_t`) format string used
internally by curl.

To work around this problem, override the format check style in curl
public headers to use `gnu_printf`. This is compatible with `%z` in all
mingw-w64 versions and allows keeping the C99 format strings internally.

Also:
- lib/ws.c: add missing space to an error message.
- docs/examples/ftpgetinfo.c: fix to use standard printf.

Ref: #14643 (take 1)
Follow-up to 3829759bd042c03225ae862062560f568ba1a231 #12489

Closes #14703

12 files changed:
configure.ac
docs/examples/CMakeLists.txt
docs/examples/Makefile.am
docs/examples/ftpgetinfo.c
include/curl/curl.h
include/curl/mprintf.h
lib/curl_printf.h
lib/curl_setup.h
lib/mprintf.c
lib/ws.c
tests/http/clients/CMakeLists.txt
tests/http/clients/Makefile.am

index 6db5e6b5aff580797637bdafde6a4ab00f4767d1..6bf47e2cf9725547d7a2387b4260c7ea8ee4ec1c 100644 (file)
@@ -597,16 +597,6 @@ case $host_os in
     ;;
 esac
 
-have_mingw='no'
-case $host_os in
-  mingw*)
-    have_mingw='yes'
-    ;;
-esac
-
-AM_CONDITIONAL([HAVE_MINGW],
-  [test "$curl_cv_native_windows" = 'yes' -a "$have_mingw" = 'yes'])
-
 AM_CONDITIONAL([HAVE_WINDRES],
   [test "$curl_cv_native_windows" = "yes" && test -n "${RC}"])
 
index 75784ab501891fb64fb0b8e694d785ecc24c3a00..ea2b8035c74a7170e85e3cae460585fe5e678358 100644 (file)
@@ -34,10 +34,6 @@ foreach(_target IN LISTS check_PROGRAMS)
   if(LIB_SELECTED STREQUAL LIB_STATIC AND WIN32)
     set_property(TARGET ${_target_name} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
   endif()
-  if(MINGW)
-    # For mingw-w64 7.0.0 and earlier to avoid '-Wformat='
-    set_property(TARGET ${_target_name} APPEND PROPERTY COMPILE_DEFINITIONS "__USE_MINGW_ANSI_STDIO=1")
-  endif()
   set_target_properties(${_target_name} PROPERTIES
     OUTPUT_NAME "${_target}" UNITY_BUILD OFF
     PROJECT_LABEL "Example ${_target}")
index 8a731f55ff5795689fb405108242bc767dd2511b..91f90cf65bca1f89743c2fcccfd071a739b171cc 100644 (file)
@@ -46,11 +46,6 @@ if USE_CPPFLAG_CURL_STATICLIB
 AM_CPPFLAGS += -DCURL_STATICLIB
 endif
 
-if HAVE_MINGW
-# For mingw-w64 7.0.0 and earlier to avoid '-Wformat='
-AM_CPPFLAGS += -D__USE_MINGW_ANSI_STDIO=1
-endif
-
 # Prevent LIBS from being used for all link targets
 LIBS = $(BLANK_AT_MAKETIME)
 
index f1a3cefd629d12c9be0dc34abd6594ed16592146..be24fa71ef303fea848f4a6e4fd51764e8bcad99 100644 (file)
@@ -75,8 +75,8 @@ int main(void)
       res = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,
                               &filesize);
       if((CURLE_OK == res) && (filesize>0))
-        curl_mprintf("filesize %s: %" CURL_FORMAT_CURL_OFF_T " bytes\n",
-                     filename, filesize);
+        printf("filesize %s: %" CURL_FORMAT_CURL_OFF_T " bytes\n",
+               filename, filesize);
     }
     else {
       /* we failed */
index dae533e32dd8c72e2d2054230b1a7e2eacf4396c..c4fae4d4463b8bf365b01e24c8d548f058ec69fd 100644 (file)
@@ -3236,7 +3236,9 @@ CURL_EXTERN CURLcode curl_easy_pause(CURL *handle, int bitmask);
 #include "options.h"
 #include "header.h"
 #include "websockets.h"
+#ifndef CURL_SKIP_INCLUDE_MPRINTF
 #include "mprintf.h"
+#endif
 
 /* the typechecker does not work in C++ (yet) */
 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && \
index e6bc5bca4701589c690fed2f5dc12fed9faca252..88059c851fb59241247c32c1fbaf07e198e43278 100644 (file)
 extern "C" {
 #endif
 
+#ifndef CURL_TEMP_PRINTF
 #if (defined(__GNUC__) || defined(__clang__) ||                         \
   defined(__IAR_SYSTEMS_ICC__)) &&                                      \
   defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) &&         \
   !defined(CURL_NO_FMT_CHECKS)
 #if defined(__MINGW32__) && !defined(__clang__)
+#if defined(__MINGW_PRINTF_FORMAT)  /* mingw-w64 3.0.0+. Needs stdio.h. */
 #define CURL_TEMP_PRINTF(fmt, arg) \
-  __attribute__((format(gnu_printf, fmt, arg)))
+  __attribute__((format(__MINGW_PRINTF_FORMAT, fmt, arg)))
+#else
+#define CURL_TEMP_PRINTF(fmt, arg)
+#endif
 #else
 #define CURL_TEMP_PRINTF(fmt, arg) \
   __attribute__((format(printf, fmt, arg)))
@@ -46,6 +51,7 @@ extern "C" {
 #else
 #define CURL_TEMP_PRINTF(fmt, arg)
 #endif
+#endif
 
 CURL_EXTERN int curl_mprintf(const char *format, ...)
   CURL_TEMP_PRINTF(1, 2);
index df752258bf0732b94fb5736dbe955f09b8070335..e851b14a5076901f1e853bf1cdc05a3922450946 100644 (file)
  * *rintf() functions.
  */
 
+#ifndef CURL_TEMP_PRINTF
+#error "CURL_TEMP_PRINTF must be set before including curl/mprintf.h"
+#endif
+
 #include <curl/mprintf.h>
 
 #define MERR_OK        0
index dc39bb123e2e5a65f38e218d1466d2192e7c1745..b6f50fec32e83f375507aba3fb41871f2b6bf3ef 100644 (file)
@@ -28,6 +28,9 @@
 #define CURL_NO_OLDIES
 #endif
 
+/* Tell "curl/curl.h" not to include "curl/mprintf.h" */
+#define CURL_SKIP_INCLUDE_MPRINTF
+
 /* FIXME: Delete this once the warnings have been fixed. */
 #if !defined(CURL_WARN_SIGN_CONVERSION)
 #ifdef __GNUC__
 #define CURL_PRINTF(fmt, arg)
 #endif
 
+/* Override default printf mask check rules in "curl/mprintf.h" */
+#define CURL_TEMP_PRINTF CURL_PRINTF
+
 /* Workaround for mainline llvm v16 and earlier missing a built-in macro
    expected by macOS SDK v14 / Xcode v15 (2023) and newer.
    gcc (as of v14) is also missing it. */
index 89cbb87ce4fd713e87e032d4ddb797562f47f6c8..42993c71771a26308acba7e89b99a2cab956faff 100644 (file)
@@ -25,7 +25,6 @@
 #include "curl_setup.h"
 #include "dynbuf.h"
 #include "curl_printf.h"
-#include <curl/mprintf.h>
 
 #include "curl_memory.h"
 /* The last #include file should be: */
index c845f5d3e12ca079b5cca2591f177d7b07cdb134..1cfabf4ff4ded7cac472ef6bcc41b924ef471f71 100644 (file)
--- a/lib/ws.c
+++ b/lib/ws.c
@@ -558,7 +558,7 @@ static ssize_t ws_enc_write_head(struct Curl_easy *data,
 
   if(enc->payload_remain > 0) {
     /* trying to write a new frame before the previous one is finished */
-    failf(data, "WS: starting new frame with %zd bytes from last one"
+    failf(data, "WS: starting new frame with %zd bytes from last one "
                 "remaining to be sent", (ssize_t)enc->payload_remain);
     *err = CURLE_SEND_ERROR;
     return -1;
index 05fd793fc5b937c2dea54600eca28e40b934a7d8..44af35ed3cb5b3d4e282466eb239a98c5ea674ad 100644 (file)
@@ -39,10 +39,6 @@ foreach(_target IN LISTS check_PROGRAMS)
   if(LIB_SELECTED STREQUAL LIB_STATIC AND WIN32)
     set_property(TARGET ${_target_name} APPEND PROPERTY COMPILE_DEFINITIONS "CURL_STATICLIB")
   endif()
-  if(MINGW)
-    # For mingw-w64 7.0.0 and earlier to avoid '-Wformat='
-    set_property(TARGET ${_target_name} APPEND PROPERTY COMPILE_DEFINITIONS "__USE_MINGW_ANSI_STDIO=1")
-  endif()
   set_target_properties(${_target_name} PROPERTIES
     OUTPUT_NAME "${_target}" UNITY_BUILD OFF
     PROJECT_LABEL "Test client ${_target}")
index e9bdda9f8891af8d9a6fc965ceaf7cc19f8368a3..007de3c877c45d8d26028dec3362c3fd4456aaa0 100644 (file)
@@ -47,11 +47,6 @@ if USE_CPPFLAG_CURL_STATICLIB
 AM_CPPFLAGS += -DCURL_STATICLIB
 endif
 
-if HAVE_MINGW
-# For mingw-w64 7.0.0 and earlier to avoid '-Wformat='
-AM_CPPFLAGS += -D__USE_MINGW_ANSI_STDIO=1
-endif
-
 # Prevent LIBS from being used for all link targets
 LIBS = $(BLANK_AT_MAKETIME)