Merge pull request #2879 from nlohmann/issue2598

Avoid string in case of empty CBOR objects
This commit is contained in:
Niels Lohmann 2021-07-17 12:38:33 +02:00 committed by GitHub
commit 8f0a880c89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 44 deletions

View File

@ -1082,38 +1082,41 @@ class binary_reader
return false;
}
string_t key;
if (len != std::size_t(-1))
if (len != 0)
{
for (std::size_t i = 0; i < len; ++i)
string_t key;
if (len != std::size_t(-1))
{
get();
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
for (std::size_t i = 0; i < len; ++i)
{
return false;
}
get();
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
{
return false;
}
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
{
return false;
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
{
return false;
}
key.clear();
}
key.clear();
}
}
else
{
while (get() != 0xFF)
else
{
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
while (get() != 0xFF)
{
return false;
}
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
{
return false;
}
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
{
return false;
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
{
return false;
}
key.clear();
}
key.clear();
}
}

View File

@ -9298,38 +9298,41 @@ class binary_reader
return false;
}
string_t key;
if (len != std::size_t(-1))
if (len != 0)
{
for (std::size_t i = 0; i < len; ++i)
string_t key;
if (len != std::size_t(-1))
{
get();
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
for (std::size_t i = 0; i < len; ++i)
{
return false;
}
get();
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
{
return false;
}
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
{
return false;
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
{
return false;
}
key.clear();
}
key.clear();
}
}
else
{
while (get() != 0xFF)
else
{
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
while (get() != 0xFF)
{
return false;
}
if (JSON_HEDLEY_UNLIKELY(!get_cbor_string(key) || !sax->key(key)))
{
return false;
}
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
{
return false;
if (JSON_HEDLEY_UNLIKELY(!parse_cbor_internal(true, tag_handler)))
{
return false;
}
key.clear();
}
key.clear();
}
}