Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/catch2/internal/catch_run_context.cpp
diff options
context:
space:
mode:
authorMartin Hořeňovský2020-10-22 14:20:47 +0200
committerMartin Hořeňovský2020-10-23 21:21:15 +0200
commit4c8454b5ecfc744a85cac57cf96b8de078e26db5 (patch)
tree7d33dd78d720311ebca196b714f80d484160af92 /src/catch2/internal/catch_run_context.cpp
parent8878f90323f7229b391f18e467113cb654fbdc06 (diff)
downloadcatch3-4c8454b5ecfc744a85cac57cf96b8de078e26db5.tar.gz
Fix potential infinite loops in generators combined with section filter
The problem was that under specific circumstances, namely that none of their children progressed, `GeneratorTracker` will not progress. This was changed recently, to allow for code like this, where a `SECTION` follows a `GENERATE` at the same level: ```cpp SECTION("A") {} auto a = GENERATE(1, 2); SECTION("B") {} ``` However, this interacted badly with `SECTION` filters (`-c foo`), as they could deactivate all `SECTION`s below a generator, and thus stop it from progressing forever. This commit makes GeneratorTracker check whether there are any filters active, and if they are, it checks whether its section-children can ever run. Fixes #2025
Diffstat (limited to 'src/catch2/internal/catch_run_context.cpp')
-rw-r--r--src/catch2/internal/catch_run_context.cpp54
1 files changed, 47 insertions, 7 deletions
diff --git a/src/catch2/internal/catch_run_context.cpp b/src/catch2/internal/catch_run_context.cpp
index bafbbeb4..f755a57e 100644
--- a/src/catch2/internal/catch_run_context.cpp
+++ b/src/catch2/internal/catch_run_context.cpp
@@ -82,13 +82,53 @@ namespace Catch {
// `SECTION`s.
// **The check for m_children.empty cannot be removed**.
// doing so would break `GENERATE` _not_ followed by `SECTION`s.
- const bool should_wait_for_child =
- !m_children.empty() &&
- std::find_if( m_children.begin(),
- m_children.end(),
- []( TestCaseTracking::ITrackerPtr tracker ) {
- return tracker->hasStarted();
- } ) == m_children.end();
+ const bool should_wait_for_child = [&]() {
+ // No children -> nobody to wait for
+ if ( m_children.empty() ) {
+ return false;
+ }
+ // If at least one child started executing, don't wait
+ if ( std::find_if(
+ m_children.begin(),
+ m_children.end(),
+ []( TestCaseTracking::ITrackerPtr tracker ) {
+ return tracker->hasStarted();
+ } ) != m_children.end() ) {
+ return false;
+ }
+
+ // No children have started. We need to check if they _can_
+ // start, and thus we should wait for them, or they cannot
+ // start (due to filters), and we shouldn't wait for them
+ auto* parent = m_parent;
+ // This is safe: there is always at least one section
+ // tracker in a test case tracking tree
+ while ( !parent->isSectionTracker() ) {
+ parent = &( parent->parent() );
+ }
+ assert( parent &&
+ "Missing root (test case) level section" );
+
+ auto const& parentSection =
+ static_cast<SectionTracker&>( *parent );
+ auto const& filters = parentSection.getFilters();
+ // No filters -> no restrictions on running sections
+ if ( filters.empty() ) {
+ return true;
+ }
+
+ for ( auto const& child : m_children ) {
+ if ( child->isSectionTracker() &&
+ std::find( filters.begin(),
+ filters.end(),
+ static_cast<SectionTracker&>( *child )
+ .trimmedName() ) !=
+ filters.end() ) {
+ return true;
+ }
+ }
+ return false;
+ }();
// This check is a bit tricky, because m_generator->next()
// has a side-effect, where it consumes generator's current