From ff72f3886321eef8e61f9e1b64ccb19032e18549 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 6 Apr 2017 19:54:08 +0200 Subject: [PATCH] :hammer: fixed another warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Do not store eof() in a char buffer… --- src/json.hpp | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index ff3fe8263..97addceb2 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -8485,8 +8485,7 @@ class basic_json { public: cached_input_stream_adapter(std::istream& i, const size_t buffer_size) - : is(i), start_position(is.tellg()), - buffer(buffer_size, std::char_traits::eof()) + : is(i), start_position(is.tellg()), buffer(buffer_size, '\0') { // immediately abort if stream is erroneous if (JSON_UNLIKELY(i.fail())) @@ -8494,12 +8493,13 @@ class basic_json JSON_THROW(parse_error::create(111, 0, "bad input stream")); } - // initial fill; unfilled buffer characters remain EOF + // initial fill is.read(buffer.data(), static_cast(buffer.size())); + // store number of bytes in the buffer + fill_size = static_cast(is.gcount()); // skip byte-order mark - assert(buffer.size() >= 3); - if (buffer[0] == '\xEF' and buffer[1] == '\xBB' and buffer[2] == '\xBF') + if (fill_size >= 3 and buffer[0] == '\xEF' and buffer[1] == '\xBB' and buffer[2] == '\xBF') { buffer_pos += 3; processed_chars += 3; @@ -8516,22 +8516,28 @@ class basic_json int get_character() override { - // check if refilling is necessary - if (JSON_UNLIKELY(buffer_pos == buffer.size())) + // check if refilling is necessary and possible + if (buffer_pos == fill_size and not eof) { // refill - is.read(reinterpret_cast(buffer.data()), static_cast(buffer.size())); - // set unfilled characters to EOF - std::fill_n(buffer.begin() + static_cast(is.gcount()), - buffer.size() - static_cast(is.gcount()), - std::char_traits::eof()); + is.read(buffer.data(), static_cast(buffer.size())); + // store number of bytes in the buffer + fill_size = static_cast(is.gcount()); + + // remember that filling did not yield new input + if (fill_size == 0) + { + eof = true; + } + // the buffer is ready buffer_pos = 0; } ++processed_chars; - const int res = buffer[buffer_pos++]; - return (res == std::char_traits::eof()) ? res : res & 0xFF; + return eof + ? std::char_traits::eof() + : buffer[buffer_pos++] & 0xFF; } std::string read(size_t offset, size_t length) override @@ -8568,6 +8574,11 @@ class basic_json /// chars processed in the current buffer size_t buffer_pos = 0; + /// whether stream reached eof + bool eof = false; + /// how many chars have been copied to the buffer by last (re)fill + size_t fill_size = 0; + /// position of the stream when we started const std::streampos start_position;