Fixture's destructor called before retrieving uncaught exception's message
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start by reproducing the issue with the provided TEST_CASE_METHOD example and trace Catch2's fixture teardown and unexpected-exception reporting paths. Done means the exception message is retrieved before the fixture is destroyed, with a regression test covering an exception whose what() depends on fixture state.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- C++
- Stars
- 21.6k
- Forks
- 3.4k
- Avg merge
- 3d 12h
- Merged PRs (30d)
- 5
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from catchorg/Catch2
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
Maintainers usually reply within 2 days
-
catch_discover_tests passes all EXTRA_ARGS as one argumentPossibly taken @CyberKun claimed this 11 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
catchorg/Catch2#3239 · 1 comment ·
Maintainers usually reply within 2 days
-
Not seeing some error messages from fakeit in jetbrains riderPossibly taken @Amanpal9 claimed this 21 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
catchorg/Catch2#3228 · 1 comment ·
Maintainers usually reply within 2 days
-
catch_discover_tests fails for Emscripten/WASM tests since v3.16.0Possibly taken @nafiurahman00 claimed this 8 days ago. OpenExtras
Difficulty 3/5 1-2 days Newbie friendliness 72/100
catchorg/Catch2#3241 · 1 comment ·
Maintainers usually reply within 2 days
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
Maintainers usually reply within 2 days
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
cp-algorithms/cp-algorithms#1774 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
telefonicaid/fiware-orion#4836 ·
Maintainers usually reply within 1 day
-
nixd exits at startup when its parent process belongs to another userPossibly taken @conao3 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
nix-community/nixd#930 ·
Maintainers usually reply within 2 days
-
refactor topic-ui topic-weather
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Command-Install Needs-Triage
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
microsoft/winget-cli#6718 ·
Maintainers usually reply within 2 days