diff --git a/include/nlohmann/detail/input/binary_reader.hpp b/include/nlohmann/detail/input/binary_reader.hpp index a650b2d01f..0b649d0289 100644 --- a/include/nlohmann/detail/input/binary_reader.hpp +++ b/include/nlohmann/detail/input/binary_reader.hpp @@ -2271,15 +2271,16 @@ class binary_reader string_t& result) { bool success = true; - std::generate_n(std::back_inserter(result), len, [this, &success, &format]() + for (NumberType i = 0; i < len; i++) { get(); if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string"))) { success = false; + break; } - return std::char_traits::to_char_type(current); - }); + result.push_back(std::char_traits::to_char_type(current)); + }; return success; } @@ -2303,15 +2304,16 @@ class binary_reader binary_t& result) { bool success = true; - std::generate_n(std::back_inserter(result), len, [this, &success, &format]() + for (NumberType i = 0; i < len; i++) { get(); if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary"))) { success = false; + break; } - return static_cast(current); - }); + result.push_back(static_cast(current)); + } return success; } diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 1d8b5fb612..2057c5432e 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -8136,15 +8136,16 @@ class binary_reader string_t& result) { bool success = true; - std::generate_n(std::back_inserter(result), len, [this, &success, &format]() + for (NumberType i = 0; i < len; i++) { get(); if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string"))) { success = false; + break; } - return std::char_traits::to_char_type(current); - }); + result.push_back(std::char_traits::to_char_type(current)); + }; return success; } @@ -8168,15 +8169,16 @@ class binary_reader binary_t& result) { bool success = true; - std::generate_n(std::back_inserter(result), len, [this, &success, &format]() + for (NumberType i = 0; i < len; i++) { get(); if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary"))) { success = false; + break; } - return static_cast(current); - }); + result.push_back(static_cast(current)); + } return success; } diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index 7415f68a95..b33c39ec17 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -1954,6 +1954,16 @@ TEST_CASE("regression tests") auto val = j.value("x", defval); auto val2 = j.value("y", defval); } + + SECTION("issue #2293 - eof doesnt cause parsing to stop") + { + std::vector data = + { + 0x7B, 0x6F, 0x62, 0x6A, 0x65, 0x63, 0x74, 0x20, 0x4F, 0x42 + }; + json result = json::from_cbor(data, true, false); + CHECK(result.is_discarded()); + } } #if !defined(JSON_NOEXCEPTION)