Skip to content
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

fix iterator_input_adapter consuming input before it should #3442

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions include/nlohmann/detail/input/input_adapters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class input_stream_adapter
};
#endif // JSON_NO_IO


// General-purpose iterator-based adapter. It might not be as fast as
// theoretically possible for some containers, but it is extremely versatile.
template<typename IteratorType>
Expand All @@ -132,30 +133,46 @@ class iterator_input_adapter
using char_type = typename std::iterator_traits<IteratorType>::value_type;

iterator_input_adapter(IteratorType first, IteratorType last)
: current(std::move(first)), end(std::move(last))
: current(std::move(first)), end(std::move(last)), current_has_been_consumed(false)
{}

typename std::char_traits<char_type>::int_type get_character()
{
if (JSON_HEDLEY_LIKELY(current != end))

if (JSON_HEDLEY_LIKELY(current_has_been_consumed))
{
auto result = std::char_traits<char_type>::to_int_type(*current);
std::advance(current, 1);
return result;
}

return std::char_traits<char_type>::eof();
if (JSON_HEDLEY_LIKELY(current != end))
{
current_has_been_consumed = true;
return std::char_traits<char_type>::to_int_type(*current);
}
else
{
current_has_been_consumed = false;
return std::char_traits<char_type>::eof();
}

}

private:
IteratorType current;
IteratorType end;
mutable IteratorType current;
const IteratorType end;
mutable bool current_has_been_consumed;

template<typename BaseInputAdapter, size_t T>
friend struct wide_string_input_helper;

bool empty() const
{
if (JSON_HEDLEY_LIKELY(current_has_been_consumed))
{
std::advance(current, 1);
current_has_been_consumed = false;
}

return current == end;
}
};
Expand Down
31 changes: 24 additions & 7 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5533,6 +5533,7 @@ class input_stream_adapter
};
#endif // JSON_NO_IO


// General-purpose iterator-based adapter. It might not be as fast as
// theoretically possible for some containers, but it is extremely versatile.
template<typename IteratorType>
Expand All @@ -5542,30 +5543,46 @@ class iterator_input_adapter
using char_type = typename std::iterator_traits<IteratorType>::value_type;

iterator_input_adapter(IteratorType first, IteratorType last)
: current(std::move(first)), end(std::move(last))
: current(std::move(first)), end(std::move(last)), current_has_been_consumed(false)
Copy link
Contributor

@falbrechtskirchinger falbrechtskirchinger Apr 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may as well remove the member initializer here, since you've already initialized it in the declaration.

I don't know if clang-tidy, etc. will complain. There're various checks for redundant stuff.

Edit: clang-tidy did in fact complain:
error: member initializer for 'current_has_been_consumed' is redundant

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is better to keep it in the constructor (maybe explicitly delete the default constructor) and remove the initialisation in the declaration?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it is better to keep it in the constructor (maybe explicitly delete the default constructor) and remove the initialisation in the declaration?

Pick one. I prefer in-class myself, so I'd remove the member initializer as suggested.

The default constructor is implicitly deleted because we have a user-defined constructor, right?

Copy link
Author

@imwhocodes imwhocodes Apr 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default constructor is implicitly deleted because we have a user-defined constructor, right?

Yes, this is actually correct

{}

typename std::char_traits<char_type>::int_type get_character()
{
if (JSON_HEDLEY_LIKELY(current != end))

if (JSON_HEDLEY_LIKELY(current_has_been_consumed))
{
auto result = std::char_traits<char_type>::to_int_type(*current);
std::advance(current, 1);
return result;
}

return std::char_traits<char_type>::eof();
if (JSON_HEDLEY_LIKELY(current != end))
{
current_has_been_consumed = true;
return std::char_traits<char_type>::to_int_type(*current);
}
else
{
current_has_been_consumed = false;
return std::char_traits<char_type>::eof();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And clang-tidy doesn't like else after return. It wants this instead:

if (JSON_HEDLEY_LIKELY(current != end))
{
    current_has_been_consumed = true;
    return std::char_traits<char_type>::to_int_type(*current);
}

current_has_been_consumed = false;
return std::char_traits<char_type>::eof();


}

private:
IteratorType current;
IteratorType end;
mutable IteratorType current;
const IteratorType end;
mutable bool current_has_been_consumed;
Copy link
Contributor

@falbrechtskirchinger falbrechtskirchinger Apr 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/__w/json/json/include/nlohmann/detail/input/input_adapters.hpp:163:18: error: use default member initializer for 'current_has_been_consumed' [modernize-use-default-member-init,-warnings-as-errors]
    mutable bool current_has_been_consumed;
                 ^
                                          {false}

clang-tidy seems to insist on this:
mutable bool current_has_been_consumed{false};

(Didn't know/remember that.)

CI should turn green after these two changes.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use

mutable bool current_has_been_consumed = false;

This library has a troubled relationship to braced initializers... :-)


template<typename BaseInputAdapter, size_t T>
friend struct wide_string_input_helper;

bool empty() const
{
if (JSON_HEDLEY_LIKELY(current_has_been_consumed))
{
std::advance(current, 1);
current_has_been_consumed = false;
}

return current == end;
}
};
Expand Down