From 935b1bd4544a23a91d68ffb9f86983e92747e9a5 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Thu, 22 Sep 2022 11:52:08 +0200 Subject: [PATCH] mprintf: use snprintf if available This is the single place in libcurl code where it uses the "native" s(n)printf() function. Used for writing floats. The use has been reviewed and vetted and uses a HUGE target buffer, but switching to snprintf() still makes this safer and removes build-time warnings. Reported-by: Philip Heiduck Fixes #9569 Closes #9570 --- CMakeLists.txt | 5 +++++ configure.ac | 1 + lib/config-win32.h | 5 +++++ lib/mprintf.c | 4 ++++ 4 files changed, 15 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 564c4dbc50818f..5fa8956febaab5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1079,6 +1079,11 @@ check_symbol_exists(getrlimit "${CURL_INCLUDES}" HAVE_GETRLIMIT) check_symbol_exists(setlocale "${CURL_INCLUDES}" HAVE_SETLOCALE) check_symbol_exists(setmode "${CURL_INCLUDES}" HAVE_SETMODE) check_symbol_exists(setrlimit "${CURL_INCLUDES}" HAVE_SETRLIMIT) + +if(NOT MSVC OR (MSVC_VERSION GREATER_EQUAL 1900)) + # earlier MSVC compilers had faulty snprintf implementations + check_symbol_exists(snprintf "${CURL_INCLUDES}" HAVE_SNPRINTF) +endif() check_function_exists(mach_absolute_time HAVE_MACH_ABSOLUTE_TIME) check_symbol_exists(inet_pton "${CURL_INCLUDES}" HAVE_INET_PTON) diff --git a/configure.ac b/configure.ac index 1f114002b15db3..9739eab2c216fd 100644 --- a/configure.ac +++ b/configure.ac @@ -3535,6 +3535,7 @@ AC_CHECK_FUNCS([fnmatch \ setlocale \ setmode \ setrlimit \ + snprintf \ utime \ utimes ],[ diff --git a/lib/config-win32.h b/lib/config-win32.h index 0ac529d25017dc..7c9cdf1fc2c2df 100644 --- a/lib/config-win32.h +++ b/lib/config-win32.h @@ -254,6 +254,11 @@ /* Define to the function return type for send. */ #define SEND_TYPE_RETV int +/* Define to 1 if you have the snprintf function. */ +#if defined(_MSC_VER) && (_MSC_VER >= 1900) +#define HAVE_SNPRINTF 1 +#endif + /* ---------------------------------------------------------------- */ /* TYPEDEF REPLACEMENTS */ /* ---------------------------------------------------------------- */ diff --git a/lib/mprintf.c b/lib/mprintf.c index 6bf55f661e5275..24c1dd555e6ff0 100644 --- a/lib/mprintf.c +++ b/lib/mprintf.c @@ -964,7 +964,11 @@ static int dprintf_formatf( #endif /* NOTE NOTE NOTE!! Not all sprintf implementations return number of output characters */ +#ifdef HAVE_SNPRINTF + (snprintf)(work, sizeof(work), formatbuf, p->data.dnum); +#else (sprintf)(work, formatbuf, p->data.dnum); +#endif #ifdef __clang__ #pragma clang diagnostic pop #endif