-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default initialized iterators are not comparable #4493
Comments
I understand the issue, and this seems to be a bigger change:
I am not sure why we introduced these requirements in the first place, but I fear that changing them now would be a breaking change which cannot be introduced in the 3.x release scheme. Any ideas on this? |
Currently, this prevents use of JSON iterator from being used like other STL iterators in certain use-cases since the JSON iterator violates the Looking at the code, it seems like the requirement may have been introduced mainly to guard the template < typename IterImpl, detail::enable_if_t < (std::is_same<IterImpl, iter_impl>::value || std::is_same<IterImpl, other_iter_impl>::value), std::nullptr_t > = nullptr >
bool operator==(const IterImpl& other) const
{
// if objects are not the same, the comparison is undefined
if (JSON_HEDLEY_UNLIKELY(m_object != other.m_object))
{
JSON_THROW(invalid_iterator::create(212, "cannot compare iterators of different containers", m_object));
}
+ if (m_object == nullptr)
+ {
+ return true;
+ }
-
- JSON_ASSERT(m_object != nullptr);
switch (m_object->m_data.m_type)
{ It may be worth trying the change and seeing which unit tests break. I wouldn't expect any unit tests to break except for a test that explicitly tries to compare two value-initialized iterators. Do you see other possible scenarios where comparison of two value-initialized iterators may break the existing code? Can you provide any such examples to help me understand the concern/severity a little better? |
Thanks for the input. I will have a look. |
Should also do the same for json/include/nlohmann/detail/iterators/iter_impl.hpp Lines 514 to 524 in a97041a
|
This will change an assert in debug and undefined behavior in release (dereferencing a |
Description
I have a use-case where I iterate over collections in a generic manner (using templates). However, iteration over basic_json fails due to the statement:
Before asserting for non-null object, shouldn't we first check of
other.m_object
is null too? If both are null, the operator should return true before the assertion.Reproduction steps
nlohmann::json::iterator{} == nlohmann::json::iterator{}
Expected vs. actual results
Expected: Should return true of m_object is nullptr for both objects
Actual: Assertion fails
Minimal code example
No response
Error messages
No response
Compiler and operating system
gcc 9.3
Library version
3.11.3
Validation
develop
branch is used.The text was updated successfully, but these errors were encountered: