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

Aperta
#2,868 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
cpp
Ambito
testing

Direzione di ricerca

Inizia riproducendo il problema con l'esempio TEST_CASE_METHOD fornito e tracciando i percorsi di teardown del fixture e di segnalazione delle eccezioni impreviste di Catch2. Il lavoro è completato quando il messaggio dell'eccezione viene recuperato prima che il fixture venga distrutto, con un test di regressione che copra un'eccezione il cui what() dipende dallo stato del fixture.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
C++
Stelle
21.6k
Fork
3.4k
Merge medio
3g 12h
PR unite (30g)
5

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di catchorg/Catch2

Tutte le issue di catchorg/Catch2

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.