improved test coverage

As we guarantee proper UTF-8 before, we do not need to cope with it later.
This commit is contained in:
Niels Lohmann 2017-12-12 20:44:57 +01:00
parent 569d275f65
commit 8419bfbbd2
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
3 changed files with 7 additions and 15 deletions

View file

@ -6415,12 +6415,8 @@ class serializer
if (ensure_ascii and (s[i] & 0x80 or s[i] == 0x7F)) if (ensure_ascii and (s[i] & 0x80 or s[i] == 0x7F))
{ {
const auto bytes = bytes_following(static_cast<uint8_t>(s[i])); const auto bytes = bytes_following(static_cast<uint8_t>(s[i]));
if (bytes == std::string::npos) // invalid characters will be detected by throw_if_invalid_utf8
{ assert (bytes != std::string::npos);
// invalid characters are treated as is, so no
// additional space will be used
break;
}
if (bytes == 3) if (bytes == 3)
{ {
@ -6588,12 +6584,8 @@ class serializer
(ensure_ascii and (s[i] & 0x80 or s[i] == 0x7F))) (ensure_ascii and (s[i] & 0x80 or s[i] == 0x7F)))
{ {
const auto bytes = bytes_following(static_cast<uint8_t>(s[i])); const auto bytes = bytes_following(static_cast<uint8_t>(s[i]));
if (bytes == std::string::npos) // invalid characters will be detected by throw_if_invalid_utf8
{ assert (bytes != std::string::npos);
// copy invalid character as is
result[pos++] = s[i];
break;
}
// check that the additional bytes are present // check that the additional bytes are present
assert(i + bytes < s.size()); assert(i + bytes < s.size());

View file

@ -1309,7 +1309,7 @@ TEST_CASE("regression tests")
SECTION("issue #838 - incorrect parse error with binary data in keys") 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 }; 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])); std::string key1_str(key1, key1 + sizeof(key1) / sizeof(key1[0]));
json j = key1_str; json j = key1_str;
CHECK_THROWS_AS(j.dump(), json::type_error); 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"); CHECK_THROWS_WITH(j.dump(), "[json.exception.type_error.316] invalid UTF-8 byte at index 10: 0x7E");