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

Fixture's destructor called before retrieving uncaught exception's message #2868

Open
nowakowsk opened this issue May 2, 2024 · 0 comments

Comments

@nowakowsk
Copy link

I have a unit test project in which exceptions rely on test fixture's state. This can lead to use-after-free situations, because uncaught exception objects can outlive fixture. Following code illustrates the problem:

#include <iostream>
#include <exception>
#include <catch2/catch_test_macros.hpp>


struct Context
{
  Context() { std::cout << "Context::Context\n"; }
  ~Context() { std::cout << "Context::~Context\n"; }
};


struct ErrorUsingContext : std::exception
{
  ErrorUsingContext(Context&) { std::cout << "ErrorUsingContext::ErrorUsingContext\n"; }
  ~ErrorUsingContext() { std::cout << "ErrorUsingContext::~ErrorUsingContext\n"; }

  const char* what() const noexcept override
  {
    std::cout << "ErrorUsingContext::what\n";
    return "description allocated and managed by Context";
  }
};


struct Test
{
  Context ctx;
};


TEST_CASE_METHOD(Test, "Test")
{
  throw ErrorUsingContext(ctx);
}

Godbolt: https://godbolt.org/z/xf3x65rhe

Output:

Context::Context
ErrorUsingContext::ErrorUsingContext
Context::~Context
ErrorUsingContext::what

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
output.s is a Catch v3.0.0-preview.3 host application.
Run with -? for options

-------------------------------------------------------------------------------
Test
-------------------------------------------------------------------------------
/app/example.cpp:32
...............................................................................

/app/example.cpp:32: FAILED:
due to unexpected exception with message:
  description allocated and managed by Context

ErrorUsingContext::~ErrorUsingContext
===============================================================================
test cases: 1 | 1 failed
assertions: 1 | 1 failed

ErrorUsingContext::what is called after Context::~Context which is a problem for me, because, in my case, memory of exception's description string is allocated and managed by the Context (strings use custom allocators).

I can work around it, but this could be avoided if uncaught exceptions' description were retrieved and saved by Catch2 before calling fixture's destructor.

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

1 participant