💥 CBOR/MessagePack input must end with EOF #505

The CBOR and MessagePack parsers now expect the input to be read until the end. Unless the new parameter "strict" is set to false (it is true by default), an exception is raised if the parser ends prematurely. This is a breaking change as the parsers ignored unread input so far.

Furthermore, the offset/startIndex paramter introduced in #462 was removed as this behavior can be mimicked with an iterator range. For instance, instead of calling "from_cbor(vec, 5);", you can write "from_cbor({vec.begin()+5, vec.end()});".
This commit is contained in:
Niels Lohmann 2017-08-16 14:48:23 +02:00
parent 1f31a5b808
commit 22b59693f1
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
4 changed files with 86 additions and 51 deletions

View file

@ -1260,6 +1260,23 @@ TEST_CASE("CBOR")
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0xa1, 0xff, 0x01})),
"[json.exception.parse_error.113] parse error at 2: expected a CBOR string; last byte: 0xff");
}
SECTION("strict mode")
{
std::vector<uint8_t> vec = {0xf6, 0xf6};
SECTION("non-strict mode")
{
const auto result = json::from_cbor(vec, false);
CHECK(result == json());
}
SECTION("strict mode")
{
CHECK_THROWS_AS(json::from_cbor(vec), json::parse_error);
CHECK_THROWS_WITH(json::from_cbor(vec),
"[json.exception.parse_error.110] parse error at 2: expected end of input");
}
}
}
}
@ -1305,7 +1322,7 @@ TEST_CASE("single CBOR roundtrip")
// check with different start index
packed.insert(packed.begin(), 5, 0xff);
CHECK(j1 == json::from_cbor(packed, 5));
CHECK(j1 == json::from_cbor({packed.begin() + 5, packed.end()}));
}
}
@ -1654,7 +1671,7 @@ TEST_CASE("all first bytes", "[!throws]")
try
{
json::from_cbor({byte});
json::from_cbor(std::vector<uint8_t>(1, byte));
}
catch (const json::parse_error& e)
{

View file

@ -1101,6 +1101,23 @@ TEST_CASE("MessagePack")
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0x81, 0xff, 0x01})),
"[json.exception.parse_error.113] parse error at 2: expected a MessagePack string; last byte: 0xff");
}
SECTION("strict mode")
{
std::vector<uint8_t> vec = {0xc0, 0xc0};
SECTION("non-strict mode")
{
const auto result = json::from_msgpack(vec, false);
CHECK(result == json());
}
SECTION("strict mode")
{
CHECK_THROWS_AS(json::from_msgpack(vec), json::parse_error);
CHECK_THROWS_WITH(json::from_msgpack(vec),
"[json.exception.parse_error.110] parse error at 2: expected end of input");
}
}
}
}
@ -1147,7 +1164,7 @@ TEST_CASE("single MessagePack roundtrip")
// check with different start index
packed.insert(packed.begin(), 5, 0xff);
CHECK(j1 == json::from_msgpack(packed, 5));
CHECK(j1 == json::from_msgpack({packed.begin() + 5, packed.end()}));
}
}

View file

@ -1108,7 +1108,7 @@ TEST_CASE("regression tests")
SECTION("issue #504 - assertion error (OSS-Fuzz 856)")
{
std::vector<uint8_t> vec1 = {0xf9, 0xff, 0xff, 0x4a, 0x3a, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x37, 0x02, 0x38};
json j1 = json::from_cbor(vec1);
json j1 = json::from_cbor(vec1, false);
// step 2: round trip
std::vector<uint8_t> vec2 = json::to_cbor(j1);