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

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

‎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-
varfound=false;
65-
varcurrentType=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-
returnfalse;
70+
// Check if any base types match the generic type definition
71+
varfound=false;
72+
varcurrentType=testClassType.BaseType;// Start with base type since we already checked the current type
73+
vardepth=0;
74+
constintmaxInheritanceDepth=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+
returnfalse;
89+
}
7990
}
8091

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

0 commit comments

Comments
 (0)