Skip to content

Commit bba2a79

Browse files
Fix infinite loop potential in TestDependency inheritance traversal
- Add depth limit (50) to prevent deep inheritance chain performance issues - Add early return optimization for direct generic type matches - Resolves engine test timeout in CI by preventing excessive inheritance traversal Co-authored-by: Tom Longhurst <thomhurst@users.noreply.github.com>
1 parent 732ba4f commit bba2a79

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

TUnit.Core/TestDependency.cs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,33 @@ public bool Matches(TestMetadata test, TestMetadata? dependentTest = null)
6060

6161
if (ClassType.IsGenericTypeDefinition)
6262
{
63-
// Check if the test class type or any of its base types match the generic type definition
64-
var found = false;
65-
var currentType = testClassType;
66-
67-
while (currentType != null && !found)
63+
// Quick check: if test class type is generic and matches directly, we're done
64+
if (testClassType.IsGenericType && testClassType.GetGenericTypeDefinition() == ClassType)
6865
{
69-
if (currentType.IsGenericType && currentType.GetGenericTypeDefinition() == ClassType)
70-
{
71-
found = true;
72-
}
73-
currentType = currentType.BaseType;
66+
// Match found, continue to method checks
7467
}
75-
76-
if (!found)
68+
else
7769
{
78-
return false;
70+
// Check if any base types match the generic type definition
71+
var found = false;
72+
var currentType = testClassType.BaseType; // Start with base type since we already checked the current type
73+
var depth = 0;
74+
const int maxInheritanceDepth = 50; // Safeguard against deep inheritance chains
75+
76+
while (currentType != null && !found && depth < maxInheritanceDepth)
77+
{
78+
if (currentType.IsGenericType && currentType.GetGenericTypeDefinition() == ClassType)
79+
{
80+
found = true;
81+
}
82+
currentType = currentType.BaseType;
83+
depth++;
84+
}
85+
86+
if (!found)
87+
{
88+
return false;
89+
}
7990
}
8091

8192
// For generic type definitions, we don't need to check arity against the test class

0 commit comments

Comments
 (0)