diff --git a/src/json.hpp b/src/json.hpp index f2919b12..e16f2e99 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -8915,7 +8915,7 @@ class basic_json { // avoid reading too many characters const size_t max_length = static_cast(limit - start); - return std::string(start + offset, std::min({length, max_length})); + return std::string(start + offset, std::min({length, max_length - offset})); } private: diff --git a/test/src/unit-regression.cpp b/test/src/unit-regression.cpp index ab9fa935..1ff8f987 100644 --- a/test/src/unit-regression.cpp +++ b/test/src/unit-regression.cpp @@ -1010,4 +1010,10 @@ TEST_CASE("regression tests") CHECK(not(6 <= j["a"])); CHECK(not(6 < j["a"])); } + + SECTION("issue #575 - heap-buffer-overflow (OSS-Fuzz 1400)") + { + std::vector vec = {'"', '\\', '"', 'X', '"', '"'}; + CHECK_THROWS_AS(json::parse(vec), json::parse_error); + } }