json: Segfault on nested parsing

It’s a bit difficult to provide a full repro here as it would mean tearing out a bunch of code, but the gist of the bug is as follows:

struct Foo { int x; bool y};
// define from_json; use f.x = j.at("x").get<int>(), etc
    auto j = json::parse(R"(
              { "m": {"one": {"x": 1},
                      "two": {"x": 2, "y": false }},
                "s": "arg"}
             )");
std::unordered_map<std::string, Foo> m = j.at("m");

The last line should throw IMHO, but in fact it segfaults. I think I’m on version 2.1.1.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 26 (14 by maintainers)

Most upvoted comments

Ah, so you’re saying that all the fields are in fact required, and you’re not expecting to handle the case you’ve given. I thought you actually wanted to read the sample file. Thanks for the clarification.

I didn’t, but I think it’s pretty clear that won’t cause the problem. However, I did manage to write up the following json agnostic example: https://wandbox.org/permlink/G5u9xZWCvsQLeRvz. This segfaults in gcc6.3 but not in 7 series. My readings on the matter seem to indicate that this is a standard library bug; its not safe to assume that iterators can’t throw. I’m going to do one more test to make sure that json does the expected thing with gcc7 and then I think we can probably close it as an upstream issue.