From 4c0d049c029bae281209d8aa2f681b48dcbc5850 Mon Sep 17 00:00:00 2001 From: Garux Date: Sun, 11 Jul 2021 16:28:10 +0300 Subject: [PATCH] prevent overflow in ostream_write( Decimal ) --- libs/stream/textstream.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/stream/textstream.h b/libs/stream/textstream.h index 41bdee51..4a3d547c 100644 --- a/libs/stream/textstream.h +++ b/libs/stream/textstream.h @@ -260,14 +260,14 @@ public: /// \brief Writes a floating point value to \p ostream in decimal form with trailing zeros removed. template inline TextOutputStreamType& ostream_write( TextOutputStreamType& ostream, const Decimal& decimal ){ - const int bufferSize = 22; + const std::size_t bufferSize = 22; char buf[bufferSize]; - std::size_t length = snprintf( buf, bufferSize, "%10.10lf", decimal.m_f ); + const std::size_t length = snprintf( buf, bufferSize, "%10.10lf", decimal.m_f ); const char* first = buf; for (; *first == ' '; ++first ) { } - const char* last = buf + length - 1; + const char* last = buf + std::min( length, bufferSize - 1 ) - 1; for (; *last == '0'; --last ) { }