Skip to content

Fix infinite loop in source generator exception handler#1087

Merged
EdwardCooke merged 1 commit intoaaubry:masterfrom
fdcastel:fix/generator-infinite-loop
Apr 9, 2026
Merged

Fix infinite loop in source generator exception handler#1087
EdwardCooke merged 1 commit intoaaubry:masterfrom
fdcastel:fix/generator-infinite-loop

Conversation

@fdcastel
Copy link
Copy Markdown
Contributor

Problem

The catch block in TypeFactoryGenerator.GenerateSource() walks inner exceptions using variable e, but every iteration reads from exception (the outer variable) instead of e. This produces an infinite loop that only ever prints the outermost message and never terminates.

var e = exception;
while (e != null)
{
    write(exception.Message, true);    // BUG: should be e.Message
    write(exception.StackTrace, true); // BUG: should be e.StackTrace
    write("======", true);
    e = exception.InnerException;      // BUG: should be e.InnerException
}

Fix

Replace exception with e inside the loop body for Message, StackTrace, and InnerException.

Tests Added

  • InnerExceptionTraversal_TerminatesAndCapturesAllMessages — 3-level exception chain
  • InnerExceptionTraversal_SingleException_TerminatesAfterOne — single exception
  • ErrorCommentFormat_ContainsAllExceptionDetails — validates full output format

Files Changed

  • YamlDotNet.Analyzers.StaticGenerator/TypeFactoryGenerator.cs
  • YamlDotNet.Test/Analyzers/StaticGenerator/TypeFactoryGeneratorTests.cs (new)

The catch block in TypeFactoryGenerator.GenerateSource() walked inner exceptions
using variable 'e', but every iteration read from 'exception' (the outer
variable) instead of 'e'. This produced an infinite loop that only ever printed
the outermost message and never terminated.

Replace 'exception' with 'e' inside the loop body for Message, StackTrace,
and InnerException.

Add tests validating the inner exception traversal pattern terminates correctly
and captures all messages in the chain.
@EdwardCooke EdwardCooke merged commit 8383b30 into aaubry:master Apr 9, 2026
1 check passed
This was referenced Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants