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

failed to iterator json object with reverse_iterator #100

Closed
likebeta opened this issue Jul 1, 2015 · 5 comments
Closed

failed to iterator json object with reverse_iterator #100

likebeta opened this issue Jul 1, 2015 · 5 comments

Comments

@likebeta
Copy link
Contributor

likebeta commented Jul 1, 2015

Hello @nlohmann , I've got a problem.

code:

#include "json.hpp"
using nlohmann::json;
#include <iostream>
using namespace std;

int main()
{
    json config = {
        { "111", 111 },
        { "112", 112 },
        { "113", 113 }
    };

    cout << config << endl;
    for (auto it = config.begin(); it != config.end(); ++it)
    {
        cout << it.key() << ": " << it.value() << endl;
    }

    for (auto it = config.rbegin(); it != config.rend(); ++it)
    {
        cout << it.key() << ": " << it.value() << endl;
    }

    return 0;
}

output:

{"111":111,"112":112,"113":113}
111: 111
112: 112
113: 113
Segmentation fault (core dumped)

Think you for your help!

@nlohmann
Copy link
Owner

nlohmann commented Jul 1, 2015

This seems to be related to #93.

@nlohmann nlohmann added this to the Release 3.0 RC milestone Jul 1, 2015
@nlohmann
Copy link
Owner

nlohmann commented Jul 4, 2015

Hi @likebeta,

thanks for reporting! I can reproduce the error with Clang AddressSanitizer. I'll check on #93 first and see if that also fixes this issue.

All the best,
Niels

@nlohmann
Copy link
Owner

nlohmann commented Jul 5, 2015

As far as I understand the issue right now, there seems to be an off-by-one error when using base() to link the reverse iterators to the "normal" iterators. I'm on it.

@nlohmann
Copy link
Owner

nlohmann commented Jul 6, 2015

Hi @likebeta,

I fixed the code. It seems that I never really understood the relationship between a reverse iterator and its underlying iterator accessed via base(). All I needed to do was to decrement the base() iterator before forwarding calls to key() or value().

I added your initial code as regression test.

Hope that works for you,
Niels

@likebeta
Copy link
Contributor Author

likebeta commented Jul 7, 2015

@nlohmann Well done, Thank you for your help ^_^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants