💥 throwing an exception in case dump encounters a non-UTF-8 string #838

We had a lot of issues with failing roundtrips (i.e., parse errors from serializations) in case string were stored in the library that were not UTF-8 encoded. This PR adds an exception in this case.
This commit is contained in:
Niels Lohmann 2017-12-11 22:38:05 +01:00
parent 383743c6c0
commit 569d275f65
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
6 changed files with 116 additions and 15 deletions

View file

@ -1287,10 +1287,10 @@ TEST_CASE("CBOR")
{
CHECK_THROWS_AS(json::from_cbor(std::vector<uint8_t>({0x1c})), json::parse_error&);
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0x1c})),
"[json.exception.parse_error.112] parse error at 1: error reading CBOR; last byte: 0x1c");
"[json.exception.parse_error.112] parse error at 1: error reading CBOR; last byte: 0x1C");
CHECK_THROWS_AS(json::from_cbor(std::vector<uint8_t>({0xf8})), json::parse_error&);
CHECK_THROWS_WITH(json::from_cbor(std::vector<uint8_t>({0xf8})),
"[json.exception.parse_error.112] parse error at 1: error reading CBOR; last byte: 0xf8");
"[json.exception.parse_error.112] parse error at 1: error reading CBOR; last byte: 0xF8");
}
SECTION("all unsupported bytes")
@ -1348,7 +1348,7 @@ TEST_CASE("CBOR")
{
CHECK_THROWS_AS(json::from_cbor(std::vector<uint8_t>({0xa1, 0xff, 0x01})), json::parse_error&);
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");
"[json.exception.parse_error.113] parse error at 2: expected a CBOR string; last byte: 0xFF");
}
SECTION("strict mode")

View file

@ -32,7 +32,7 @@ SOFTWARE.
#include "json.hpp"
using nlohmann::json;
void check_escaped(const char* original, const char* escaped, const bool ensure_ascii = false);
void check_escaped(const char* original, const char* escaped = "", const bool ensure_ascii = false);
void check_escaped(const char* original, const char* escaped, const bool ensure_ascii)
{
std::stringstream ss;
@ -99,7 +99,12 @@ TEST_CASE("convenience functions")
check_escaped("\x1f", "\\u001f");
// invalid UTF-8 characters
check_escaped("ä\xA9ü", "ä\xA9ü");
check_escaped("ä\xA9ü", "\\u00e4\xA9\\u00fc", true);
CHECK_THROWS_AS(check_escaped("ä\xA9ü"), json::type_error);
CHECK_THROWS_WITH(check_escaped("ä\xA9ü"),
"[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9");
CHECK_THROWS_AS(check_escaped("\xC2"), json::type_error);
CHECK_THROWS_WITH(check_escaped("\xC2"),
"[json.exception.type_error.316] incomplete UTF-8 string; last byte: 0xC2");
}
}

View file

@ -1077,10 +1077,10 @@ TEST_CASE("MessagePack")
{
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xc1})), json::parse_error&);
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xc1})),
"[json.exception.parse_error.112] parse error at 1: error reading MessagePack; last byte: 0xc1");
"[json.exception.parse_error.112] parse error at 1: error reading MessagePack; last byte: 0xC1");
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xc6})), json::parse_error&);
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xc6})),
"[json.exception.parse_error.112] parse error at 1: error reading MessagePack; last byte: 0xc6");
"[json.exception.parse_error.112] parse error at 1: error reading MessagePack; last byte: 0xC6");
}
SECTION("all unsupported bytes")
@ -1106,7 +1106,7 @@ TEST_CASE("MessagePack")
{
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0x81, 0xff, 0x01})), json::parse_error&);
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");
"[json.exception.parse_error.113] parse error at 2: expected a MessagePack string; last byte: 0xFF");
}
SECTION("strict mode")

View file

@ -975,7 +975,7 @@ TEST_CASE("regression tests")
};
CHECK_THROWS_AS(json::from_cbor(vec1), json::parse_error&);
CHECK_THROWS_WITH(json::from_cbor(vec1),
"[json.exception.parse_error.113] parse error at 13: expected a CBOR string; last byte: 0xb4");
"[json.exception.parse_error.113] parse error at 13: expected a CBOR string; last byte: 0xB4");
// related test case: double-precision
std::vector<uint8_t> vec2
@ -989,7 +989,7 @@ TEST_CASE("regression tests")
};
CHECK_THROWS_AS(json::from_cbor(vec2), json::parse_error&);
CHECK_THROWS_WITH(json::from_cbor(vec2),
"[json.exception.parse_error.113] parse error at 13: expected a CBOR string; last byte: 0xb4");
"[json.exception.parse_error.113] parse error at 13: expected a CBOR string; last byte: 0xB4");
}
SECTION("issue #452 - Heap-buffer-overflow (OSS-Fuzz issue 585)")
@ -1306,6 +1306,15 @@ TEST_CASE("regression tests")
CHECK(j["nocopy"]["val"] == 0);
}
SECTION("issue #838 - incorrect parse error with binary data in keys")
{
uint8_t key1[] = { 103, 92, 117, 48, 48, 48, 55, 92, 114, 215, 126, 214, 95, 92, 34, 174, 40, 71, 38, 174, 40, 71, 38, 223, 134, 247, 127 };
std::string key1_str(key1, key1 + sizeof(key1)/sizeof(key1[0]));
json j = key1_str;
CHECK_THROWS_AS(j.dump(), json::type_error);
CHECK_THROWS_WITH(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 10: 0x7E");
}
SECTION("issue #843 - converting to array not working")
{
json j;