Skip to content

Commit b6eb3b1

Browse files
authored
no more sprintf (#1716)
1 parent 05b07d4 commit b6eb3b1

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# testthat (development version)
22

3+
* The embedded version of Catch no longer uses `sprintf()`.
4+
35
# testthat 3.1.5
46

57
* Deprecation warnings are no longer captured by `expect_warning(code, NA)`,

inst/include/testthat/vendor/catch.h

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9473,6 +9473,10 @@ Ptr<IStreamingReporter> addReporter( Ptr<IStreamingReporter> const& existingRepo
94739473
// #included from: catch_reporter_bases.hpp
94749474
#define TWOBLUECUBES_CATCH_REPORTER_BASES_HPP_INCLUDED
94759475

9476+
#include <iomanip>
9477+
#include <iostream>
9478+
#include <sstream>
9479+
94769480
#include <cstring>
94779481
#include <cfloat>
94789482
#include <cstdio>
@@ -9481,24 +9485,10 @@ Ptr<IStreamingReporter> addReporter( Ptr<IStreamingReporter> const& existingRepo
94819485
namespace Catch {
94829486

94839487
namespace {
9484-
// Because formatting using c++ streams is stateful, drop down to C is required
9485-
// Alternatively we could use stringstream, but its performance is... not good.
94869488
std::string getFormattedDuration( double duration ) {
9487-
// Max exponent + 1 is required to represent the whole part
9488-
// + 1 for decimal point
9489-
// + 3 for the 3 decimal places
9490-
// + 1 for null terminator
9491-
const size_t maxDoubleSize = DBL_MAX_10_EXP + 1 + 1 + 3 + 1;
9492-
char buffer[maxDoubleSize];
9493-
9494-
// Save previous errno, to prevent sprintf from overwriting it
9495-
ErrnoGuard guard;
9496-
#ifdef _MSC_VER
9497-
sprintf_s(buffer, "%.3f", duration);
9498-
#else
9499-
sprintf(buffer, "%.3f", duration);
9500-
#endif
9501-
return std::string(buffer);
9489+
std::stringstream ss;
9490+
ss << std::setprecision(4) << duration;
9491+
return ss.str();
95029492
}
95039493
}
95049494

0 commit comments

Comments
 (0)