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

~basic_json can terminate program #4653

Open
2 tasks done
Ignition opened this issue Feb 17, 2025 · 0 comments · May be fixed by #4654
Open
2 tasks done

~basic_json can terminate program #4653

Ignition opened this issue Feb 17, 2025 · 0 comments · May be fixed by #4654

Comments

@Ignition
Copy link

Description

During ~basic_json, it is possible to get an exception from the allocation subsystem (quota/policy violation etc). This should be expected and accounted for.

Either:

  • ~basic_json should not be using any allocation, OR
  • upon exception a non allocating alternative to destroy should be used.

Reproduction steps

Throw any exception when allocation request is made during ~basic_json

Expected vs. actual results

Expected: ~basic_json should not terminate a program.
Actual: it terminates the program

Minimal code example

#include <nlohmann/json.hpp>
using json = nlohmann::json;

thread_local bool should_throw = false;

struct QuotaReached : std::exception {};

void* operator new(std::size_t size) {
    if (should_throw){
        throw QuotaReached{};
    }
    void* ptr = std::malloc(size);
    if (!ptr) {
        throw std::bad_alloc();
    }
    return ptr;
}

int main()
{
    {
        auto j = json{1,2,3,4};
        should_throw = true;
        // `j` is destroyed, ~basic_json is noexcept
        // allocation attempted, exception thrown
        // terminate is called
    }
}

https://godbolt.org/z/zh41cxW8j

Error messages

terminate called after throwing an instance of 'QuotaReached'
  what():  std::exception
Program terminated with signal: SIGSEGV

Compiler and operating system

gcc 14.2 Linux

Library version

3.11.1

Validation

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

Successfully merging a pull request may close this issue.

1 participant