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

fmt::format_to + exception = SIGABRT #2097

Closed
ivan-volnov opened this issue Jan 16, 2021 · 1 comment
Closed

fmt::format_to + exception = SIGABRT #2097

ivan-volnov opened this issue Jan 16, 2021 · 1 comment

Comments

@ivan-volnov
Copy link
Contributor

Hi!

I suppose I found a problem in the library.

A trivial example. I want to use a static string without any memory allocation and I want to get an exception if it fails because of the string size:

try {
    boost::beast::static_string<5> str;
    // it throws std::length_error in boost::beast::static_string::push_back()
    fmt::format_to(std::back_inserter(str), "test{}", 123);
}
catch (const std::length_error &e) {
    std::clog << "ee " << e.what() << std::endl;
}

As result, the app was terminated with SIGABRT

I think the problem is here:

~iterator_buffer() { flush(); }

While exception unwinding iterator_buffer's destructor is called and it tries to write to the buffer again

I believe you can use something like this:

// exception_count(std::uncaught_exceptions())

// in the destructor
if (exception_count == std::uncaught_exceptions()) {
    database->exec("COMMIT");
    return;
}
// Called during stack unwinding. Rollback and don't throw
try {
    database->exec("ROLLBACK");
} catch (const std::exception &e) {
    std::cerr << "Transaction rollback error: " << e.what() << std::endl;
}
@vitaut
Copy link
Contributor

vitaut commented Jan 24, 2021

Good catch, thanks! Fixed in ce519e9.

@vitaut vitaut closed this as completed Jan 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants