-
Notifications
You must be signed in to change notification settings - Fork 266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix infinite loop in exception formatting when exceptions have cyclic references #3972
Fix infinite loop in exception formatting when exceptions have cyclic references #3972
Conversation
… references The GetFormattedExceptionMessage method in ExceptionHelper.cs recursively traverses the inner exceptions of an exception to build a formatted error message string. However, it did not handle the case where the inner exceptions have a cyclic reference, causing an infinite loop. This change fixes that issue by keeping track of the exceptions seen during traversal in a HashSet. If an exception is encountered again, it breaks out of the loop and appends a "[Cyclic Exception Reference]" message to the output, avoiding the infinite recursion. The key changes are: Introduce a HashSet seenExceptions to keep track of exceptions encountered so far. Check if the current exception has already been seen. If so, append a "[Cyclic Exception Reference]" message and break out of the loop. If the current exception hasn't been seen, add it to the seenExceptions set and continue processing as before. A new test class ExceptionHelperTests has also been added with two test methods: GetFormattedExceptionMessage_WithCyclicExceptions_ShouldNotLoopInfinitely - Verifies that cyclic exceptions are handled correctly and don't cause infinite loops. GetFormattedExceptionMessage_WithNoCyclicExceptions_ShouldFormatNormally - Ensures that the normal formatting still works as expected for non-cyclic exceptions. This fixes microsoft#1234 Files changed: src/Adapter/MSTest.TestAdapter/Execution/ExceptionHelper.cs test/UnitTests/MSTestAdapter.UnitTests/Execution/ExceptionHelperTests.cs
@microsoft-github-policy-service agree |
Fix formatting
Co-authored-by: campersau <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will also need to handle the StackTrace method helper.
[TestClass] | ||
public class ExceptionHelperTests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are not using MSTest to test MSTest for some historical reason.
[TestClass] | |
public class ExceptionHelperTests | |
public sealed class ExceptionHelperTests : TestContainer |
[TestMethod] | ||
public void GetFormattedExceptionMessage_WithCyclicExceptions_ShouldNotLoopInfinitely() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[TestMethod] | |
public void GetFormattedExceptionMessage_WithCyclicExceptions_ShouldNotLoopInfinitely() | |
public void GetFormattedExceptionMessage_WithCyclicExceptions_ShouldNotLoopInfinitely() |
[TestMethod] | ||
public void GetFormattedExceptionMessage_WithNoCyclicExceptions_ShouldFormatNormally() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[TestMethod] | |
public void GetFormattedExceptionMessage_WithNoCyclicExceptions_ShouldFormatNormally() | |
public void GetFormattedExceptionMessage_WithNoCyclicExceptions_ShouldFormatNormally() |
for (Exception? curException = ex; | ||
curException != null; | ||
curException = curException.InnerException) | ||
{ | ||
if (!seenExceptions.Add(curException)) | ||
{ | ||
result.Append(" ---> [Cyclic Exception Reference]"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use localized message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will also need to handle the StackTrace method helper.
I still don't understand this. An exception can't have a cyclical inner exception reference without using reflection to hack it (unless someone can give me source code to prove me wrong?) And that would hang on the throw anyway so this wouldn't solve that? |
Good point! I assumed that the throw was good and it was hanging inside MSTest but that's the throw that's causing exception so nothing to do on MSTest side. Thanks @thomhurst |
The
GetFormattedExceptionMessage
method inExceptionHelper.cs
recursively traverses the inner exceptions of an exception to build a formatted error message string. However, it didn't handle the case where the inner exceptions have a cyclic reference, causing an infinite loop.This change fixes that issue by keeping track of the exceptions seen during traversal in a HashSet. If an exception is encountered again, it breaks out of the loop and appends a "[Cyclic Exception Reference]" message to the output, avoiding the infinite recursion.
The key changes are:
This fixes #3971