KEMBAR78
Crash when traversing over items() of temporary json objects · Issue #2040 · nlohmann/json · GitHub
Skip to content

Crash when traversing over items() of temporary json objects #2040

@BOT-Man-JL

Description

@BOT-Man-JL
  • What is the issue you have?
  1. crash when traversing over items() of temporary json objects
  2. sometimes got null of pair.value() (https://godbolt.org/z/TTQFHW)
  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?
  const auto obj = nlohmann::json::parse(R"({
    "key1": 0.1,
    "key2": 0.2
  })");

  auto rvalue = [&]() -> nlohmann::json { return obj; };
  for (const auto& pair : rvalue().items()) {
    std::cout << pair.key() << ": " << pair.value() << "\n";  // crash
  }

  auto crvalue = [&]() -> const nlohmann::json { return obj; };
  for (const auto& pair : crvalue().items()) {
    std::cout << pair.key() << ": " << pair.value() << "\n";  // crash
  }
  • What is the expected behavior?
  1. pair.value() returns 0.1 or 0.2
  2. no crash
  • And what is the actual behavior instead?
  1. crash on my linux machine: json.hpp:14028: void nlohmann::detail::serializer<BasicJsonType>::dump(const BasicJsonType&, bool, bool, unsigned int, unsigned int) [with BasicJsonType = nlohmann::basic_json<>]: Assertion ‘false' failed.
  2. pair.value() returns null on Compiler Explorer. (https://godbolt.org/z/TTQFHW)
  1. linux/centos
  2. gcc 6.2
  • Did you use a released version of the library or the version from the develop branch?
  1. latest dev branch
  2. release v3.7.3

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions