From 654552109ee6f63f32f2c9531f6891d3044309d8 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Tue, 6 Dec 2022 10:49:40 +0800 Subject: [PATCH 1/2] don't use sprintf for formatting duration --- inst/include/testthat/vendor/catch.h | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/inst/include/testthat/vendor/catch.h b/inst/include/testthat/vendor/catch.h index 47c62b191..82c5a21f2 100644 --- a/inst/include/testthat/vendor/catch.h +++ b/inst/include/testthat/vendor/catch.h @@ -9473,6 +9473,10 @@ Ptr addReporter( Ptr const& existingRepo // #included from: catch_reporter_bases.hpp #define TWOBLUECUBES_CATCH_REPORTER_BASES_HPP_INCLUDED +#include +#include +#include + #include #include #include @@ -9481,24 +9485,10 @@ Ptr addReporter( Ptr const& existingRepo namespace Catch { namespace { - // Because formatting using c++ streams is stateful, drop down to C is required - // Alternatively we could use stringstream, but its performance is... not good. std::string getFormattedDuration( double duration ) { - // Max exponent + 1 is required to represent the whole part - // + 1 for decimal point - // + 3 for the 3 decimal places - // + 1 for null terminator - const size_t maxDoubleSize = DBL_MAX_10_EXP + 1 + 1 + 3 + 1; - char buffer[maxDoubleSize]; - - // Save previous errno, to prevent sprintf from overwriting it - ErrnoGuard guard; -#ifdef _MSC_VER - sprintf_s(buffer, "%.3f", duration); -#else - sprintf(buffer, "%.3f", duration); -#endif - return std::string(buffer); + std::stringstream ss; + ss << std::setprecision(4) << duration; + return ss.str(); } } From 81aa0d1f85a65deb7d5fe039a8471e88940a50e6 Mon Sep 17 00:00:00 2001 From: Kevin Ushey Date: Tue, 6 Dec 2022 10:50:27 +0800 Subject: [PATCH 2/2] update NEWS --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 5b004c93c..a990621df 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # testthat (development version) +* The embedded version of Catch no longer uses `sprintf()`. + # testthat 3.1.5 * Deprecation warnings are no longer captured by `expect_warning(code, NA)`,