From 601ee4ba7ae9d9b532dab5f7e757f4187dce6300 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Mon, 13 Jun 2022 16:02:53 +0300 Subject: [PATCH] Use snprintf when available to avoid security warnings (refs #25) --- include/boost/assert/source_location.hpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/boost/assert/source_location.hpp b/include/boost/assert/source_location.hpp index 9ab5c29..cca87e7 100644 --- a/include/boost/assert/source_location.hpp +++ b/include/boost/assert/source_location.hpp @@ -73,6 +73,12 @@ public: #if defined(BOOST_MSVC) # pragma warning( push ) # pragma warning( disable: 4996 ) +#endif + +#if ( defined(_MSC_VER) && _MSC_VER < 1900 ) || ( defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) ) +# define BOOST_ASSERT_SNPRINTF(buffer, format, arg) std::sprintf(buffer, format, arg) +#else +# define BOOST_ASSERT_SNPRINTF(buffer, format, arg) std::snprintf(buffer, sizeof(buffer)/sizeof(buffer[0]), format, arg) #endif std::string to_string() const @@ -88,14 +94,14 @@ public: char buffer[ 16 ]; - std::sprintf( buffer, ":%lu", ln ); + BOOST_ASSERT_SNPRINTF( buffer, ":%lu", ln ); r += buffer; unsigned long co = column(); if( co ) { - std::sprintf( buffer, ":%lu", co ); + BOOST_ASSERT_SNPRINTF( buffer, ":%lu", co ); r += buffer; } @@ -111,6 +117,8 @@ public: return r; } +#undef BOOST_ASSERT_SNPRINTF + #if defined(BOOST_MSVC) # pragma warning( pop ) #endif