Guard the null value of a value-initialized iterator; drop the unneeded found flag
With the root's byte range, diagnostics() reaches get_byte_positions() also with JSON_DIAGNOSTICS when there is no value, as for a value-initialized iterator (invalid_iterator.212). get_byte_positions() now returns no range for a null value, the same change as #6003, so either can be merged first. In the view, a value that value_path() does not find leaves no tokens, so the found flag added nothing to the tokens.empty() test. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
5 files changed
+21
-6
No files matched your search
@@ -223,7 +223,8 @@ class exception : public std::exception
|
||||
template<typename BasicJsonType>
|
||||
static std::string get_byte_positions(const BasicJsonType* leaf_element)
|
||||
{
|
||||
if ((leaf_element->start_pos() != std::string::npos) && (leaf_element->end_pos() != std::string::npos))
|
||||
// a value-initialized iterator has no value (invalid_iterator.212)
|
||||
if (leaf_element != nullptr && leaf_element->start_pos() != std::string::npos && leaf_element->end_pos() != std::string::npos)
|
||||
{
|
||||
return concat("(bytes ", std::to_string(leaf_element->start_pos()), "-", std::to_string(leaf_element->end_pos()), ") ");
|
||||
}
|
||||
|
||||
@@ -255,8 +255,9 @@ std::string diagnostics(const error_site site)
|
||||
std::string context;
|
||||
#if JSON_DIAGNOSTICS
|
||||
std::vector<std::string> tokens;
|
||||
const bool found = edited ? value_path<true>(d, site.value(), tokens) : value_path<false>(d, site.value(), tokens);
|
||||
if (found && !tokens.empty())
|
||||
// a value that is not found leaves no tokens, like the root
|
||||
static_cast<void>(edited ? value_path<true>(d, site.value(), tokens) : value_path<false>(d, site.value(), tokens));
|
||||
if (!tokens.empty())
|
||||
{
|
||||
// the root value has no path, but still has a byte range
|
||||
std::string path;
|
||||
|
||||
Generated
+2
-1
@@ -5720,7 +5720,8 @@ class exception : public std::exception
|
||||
template<typename BasicJsonType>
|
||||
static std::string get_byte_positions(const BasicJsonType* leaf_element)
|
||||
{
|
||||
if ((leaf_element->start_pos() != std::string::npos) && (leaf_element->end_pos() != std::string::npos))
|
||||
// a value-initialized iterator has no value (invalid_iterator.212)
|
||||
if (leaf_element != nullptr && leaf_element->start_pos() != std::string::npos && leaf_element->end_pos() != std::string::npos)
|
||||
{
|
||||
return concat("(bytes ", std::to_string(leaf_element->start_pos()), "-", std::to_string(leaf_element->end_pos()), ") ");
|
||||
}
|
||||
|
||||
Generated
+3
-2
@@ -5097,8 +5097,9 @@ std::string diagnostics(const error_site site)
|
||||
std::string context;
|
||||
#if JSON_DIAGNOSTICS
|
||||
std::vector<std::string> tokens;
|
||||
const bool found = edited ? value_path<true>(d, site.value(), tokens) : value_path<false>(d, site.value(), tokens);
|
||||
if (found && !tokens.empty())
|
||||
// a value that is not found leaves no tokens, like the root
|
||||
static_cast<void>(edited ? value_path<true>(d, site.value(), tokens) : value_path<false>(d, site.value(), tokens));
|
||||
if (!tokens.empty())
|
||||
{
|
||||
// the root value has no path, but still has a byte range
|
||||
std::string path;
|
||||
|
||||
@@ -265,3 +265,14 @@ TEST_CASE("values read from a binary format have no positions")
|
||||
check_no_positions(j);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("comparing a value-initialized iterator throws invalid_iterator.212")
|
||||
{
|
||||
// such an iterator has no value to take the byte positions from; the
|
||||
// comparison used to dereference the null pointer instead of throwing
|
||||
json j = {1, 2};
|
||||
json::iterator it{};
|
||||
CHECK_THROWS_WITH_AS(it == j.begin(), "[json.exception.invalid_iterator.212] cannot compare iterators of different containers", json::invalid_iterator);
|
||||
CHECK_THROWS_WITH_AS(it < j.begin(), "[json.exception.invalid_iterator.212] cannot compare iterators of different containers", json::invalid_iterator);
|
||||
CHECK_THROWS_WITH_AS(j.begin() == it, "[json.exception.invalid_iterator.212] cannot compare iterators of different containers", json::invalid_iterator);
|
||||
}
|
||||
Reference in new issue
Block a user