From 96dda1b86268ca6ecd4684ccbb92d0edf19e2c78 Mon Sep 17 00:00:00 2001 From: Martchus Date: Tue, 4 Jun 2019 19:40:12 +0200 Subject: [PATCH] Prevent use of extra buffer in BinaryReader::readTerminatedString() --- io/binaryreader.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/io/binaryreader.cpp b/io/binaryreader.cpp index c1c084d..31c2077 100644 --- a/io/binaryreader.cpp +++ b/io/binaryreader.cpp @@ -88,7 +88,7 @@ string BinaryReader::readString(size_t length) { string res; res.resize(length); - m_stream->read(&res[0], length); + m_stream->read(&res[0], static_cast(length)); return res; } @@ -98,9 +98,6 @@ string BinaryReader::readString(size_t length) * Advances the current position of the stream by the string length plus one byte. * * \param termination The byte to be recognized as termination value. - * - * \deprecated This method is likely refactored/removed in v5. - * \todo Refactor/remove in v5. */ std::string BinaryReader::readTerminatedString(std::uint8_t termination) { @@ -119,20 +116,19 @@ std::string BinaryReader::readTerminatedString(std::uint8_t termination) * * \param maxBytesToRead The maximal number of bytes to read. * \param termination The value to be recognized as termination. - * - * \deprecated This method is likely refactored/removed in v5. - * \todo Refactor/remove in v5. */ string BinaryReader::readTerminatedString(std::size_t maxBytesToRead, std::uint8_t termination) { - unique_ptr buff = make_unique(maxBytesToRead); - for (char *i = buff.get(), *end = i + maxBytesToRead; i < end; ++i) { - m_stream->get(*i); - if (*(reinterpret_cast(i)) == termination) { - return string(buff.get(), static_cast(i - buff.get())); + string res; + res.reserve(maxBytesToRead); + while (res.size() < maxBytesToRead) { + const auto c = static_cast(m_stream->get()); + if (static_cast(c) == termination) { + return res; } + res += c; } - return string(buff.get(), maxBytesToRead); + return res; } /*!