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

segfault when inserting a field into pmr::json #482

Closed
1 of 3 tasks
romange opened this issue Jan 25, 2024 · 2 comments
Closed
1 of 3 tasks

segfault when inserting a field into pmr::json #482

romange opened this issue Jan 25, 2024 · 2 comments

Comments

@romange
Copy link
Contributor

romange commented Jan 25, 2024

Describe the bug

the code below crashes

  pmr::json dest;
  dest["bar"] = "foo";

should not crash

Enumerate the steps to reproduce the bug

Include a small, self-contained example if possible

What compiler, architecture, and operating system?

  • Compiler: ________
  • Architecture (e.g. x86, x64) ____
  • Operating system: linux

What jsoncons library version?

  • Latest release 0.173.2
  • Other release v0.171.1
  • master
@romange romange added the Bug label Jan 25, 2024
@romange
Copy link
Contributor Author

romange commented Jan 25, 2024

The workaround is to declare json object like this: pmr::json dest{json_object_arg};

@danielaparker
Copy link
Owner

danielaparker commented Feb 2, 2024

pmr::json dest uses the default constructor, which creates an empty object, with no space allocated for members, hence no need yet for an allocator object.

The statement

dest["bar"] = "foo";

attempts to add a member to the empty object. jsoncons now needs to allocate space for the member, but it will only do so if it detects that the allocator type is stateless, e.g. std::allocator or another allocator type that doesn't hold state. std::pmr::polymorphic_allocator is a stateful allocator, jsoncons detects that, and fails. I had intended that to be a compile time error, and so used a static_assert, but in fact it isn't detected until runtime, which is why it crashed so unceremoniously. I've replaced the static_assert with a runtime exception in the main branch.

Normally you would create a pmr::json something like

std::pmr::polymorphic_allocator<char> alloc(&pool);

pmr::json dest(json_object_arg, alloc);

Then

dest["bar"] = "foo";

would use the memory resource pool that you provided.

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