Merge pull request #2294 from jprochazk/develop

fix eof for get_binary and get_string
This commit is contained in:
Niels Lohmann 2020-07-20 08:02:03 +02:00 committed by GitHub
commit 893eda8353
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 12 deletions

View file

@ -2271,15 +2271,16 @@ class binary_reader
string_t& result) string_t& result)
{ {
bool success = true; bool success = true;
std::generate_n(std::back_inserter(result), len, [this, &success, &format]() for (NumberType i = 0; i < len; i++)
{ {
get(); get();
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string"))) if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
{ {
success = false; success = false;
break;
} }
return std::char_traits<char_type>::to_char_type(current); result.push_back(std::char_traits<char_type>::to_char_type(current));
}); };
return success; return success;
} }
@ -2303,15 +2304,16 @@ class binary_reader
binary_t& result) binary_t& result)
{ {
bool success = true; bool success = true;
std::generate_n(std::back_inserter(result), len, [this, &success, &format]() for (NumberType i = 0; i < len; i++)
{ {
get(); get();
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary"))) if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
{ {
success = false; success = false;
break;
} }
return static_cast<std::uint8_t>(current); result.push_back(static_cast<std::uint8_t>(current));
}); }
return success; return success;
} }

View file

@ -8143,15 +8143,16 @@ class binary_reader
string_t& result) string_t& result)
{ {
bool success = true; bool success = true;
std::generate_n(std::back_inserter(result), len, [this, &success, &format]() for (NumberType i = 0; i < len; i++)
{ {
get(); get();
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string"))) if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "string")))
{ {
success = false; success = false;
break;
} }
return std::char_traits<char_type>::to_char_type(current); result.push_back(std::char_traits<char_type>::to_char_type(current));
}); };
return success; return success;
} }
@ -8175,15 +8176,16 @@ class binary_reader
binary_t& result) binary_t& result)
{ {
bool success = true; bool success = true;
std::generate_n(std::back_inserter(result), len, [this, &success, &format]() for (NumberType i = 0; i < len; i++)
{ {
get(); get();
if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary"))) if (JSON_HEDLEY_UNLIKELY(!unexpect_eof(format, "binary")))
{ {
success = false; success = false;
break;
} }
return static_cast<std::uint8_t>(current); result.push_back(static_cast<std::uint8_t>(current));
}); }
return success; return success;
} }

View file

@ -1954,6 +1954,16 @@ TEST_CASE("regression tests")
auto val = j.value("x", defval); auto val = j.value("x", defval);
auto val2 = j.value("y", defval); auto val2 = j.value("y", defval);
} }
SECTION("issue #2293 - eof doesnt cause parsing to stop")
{
std::vector<uint8_t> 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) #if !defined(JSON_NOEXCEPTION)