Skip to content

Fix crashes and code fixes found by syntax fuzzing - #4344

Merged
AArnott merged 10 commits into
masterfrom
fix-fuzz-findings
Oct 19, 2026
Merged

AArnott merged 10 commits into
masterfrom
fix-fuzz-findings

Conversation

@AArnott

@AArnott AArnott commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Found while exploring #4338. These are not a fix for that issue.

What was wrong

A deterministic syntax-mutation pass and a replay of extracted test snippets found four defects:

  • b++ at the end of an incomplete buffer crashed SA1003. TokenHelper.IsFirstInLine and IsLastInLine called GetLineSpan before checking that SyntaxTree was null.
  • SA1316 renamed only the tuple element declaration. value.nameA was left unchanged and no longer compiled.
  • SA1309 renamed _testEvent to testEvent, which was already the event name, and introduced CS0102.
  • A finalizer that is not inside a type made SA1643 and SA1600 throw NullReferenceException from GetTypeParameterList.

Review of the SA1316 fix found more ways that replacement could produce invalid code:

  • A tuple element referenced from documentation trivia, such as <see cref="Point.x"/>, was not found inside trivia. The fixer replaced the token that owned the comment (the class keyword) and threw ArgumentException.
  • Renaming the identifier in new { value.nameA } also changed the inferred anonymous property name. Later uses of projected.nameA no longer compiled, and new { value.nameA, NameA = 0 } gained duplicate properties.
  • Preserving that property name with ValueText dropped the @ from an escaped keyword, so new { value.@class } became class = value.Class.
  • An inferred tuple projection such as (false, value.nameA) lost the name nameA when the element was renamed. A projection that also had an explicit NameA element could not be renamed without selecting the wrong element.
  • A Visual Basic document can reference a public C# tuple element. Replacing that reference with a C# identifier throws, and skipping the document would leave the reference broken.

What changed

  • The null check in both helpers now happens before GetLineSpan.
  • SA1316 replaces the declared name and its references directly. Roslyn's renamer throws on tuple element symbols. Tuple literal names still have no symbol and keep the token-only replacement. Reference lookup searches trivia. An inferred anonymous projection is rewritten to an explicit initializer that keeps the original identifier, including @ escaping. An inferred tuple projection uses the corrected explicit name and updates consumers of that name. The fix is not offered when that corrected name would select another element, or when any reference is not in a C# document.
  • SA1309 is not offered when the stripped name collides with an existing member.
  • SA1642, SA1643, and SA1600 are not offered when the constructor or finalizer has no containing type to name.

Tests

Each new test was run before the corresponding fix:

  • The helper tests fail with the NullReferenceException at TokenHelper.cs.
  • The SA1316 reference test fails with the declaration renamed and value.nameA left unchanged.
  • The SA1309 test fails by showing the field and its uses renamed onto the existing event.
  • The SA1643 and SA1600 tests fail with the NullReferenceException from GetTypeParameterList.
  • The cref case throws from ClassDeclarationSyntax.Update.
  • The anonymous cases produce value.NameA without keeping the original property name, and the escaped-keyword case produces class = value.Class.
  • The inferred tuple case leaves projected.nameA pointing at a name that no longer exists. The colliding tuple case and the Visual Basic case would still offer a fix.

The SA1003, SA1309, SA1600, and SA1643 suites pass on the C# 6 project. The SA1316 suite passes on the C# 7 and C# 12 projects. The new tests also pass on the C# 15 project.

Perf

The analyzer callbacks are unchanged. The helper null check already existed; it now runs before GetLineSpan. The extra symbol, trivia, and ancestor checks are in the code fixes, which run only when a fix is requested.

Behavior

  • ⚑ From C# 9, incomplete b++ reports SA1003 (++ should be followed by whitespace) instead of crashing, and the code fix inserts the space. Earlier compilers do not report it.
  • ⚑ SA1309 no longer offers a rename that would collide with another member.
  • ⚑ Documentation fixes are not offered for a constructor or finalizer that is not inside a type.
  • ⚑ SA1316 now updates references inside documentation trivia, preserves an inferred anonymous property name as an explicit initializer, and keeps @ escaping on that name.
  • ⚑ SA1316 is not offered when an inferred tuple projection would collide with another element, when a duplicate inferred name was suppressed, or when a reference is in a non-C# document. A non-colliding inferred tuple projection is renamed together with its consumers, including later projections of those consumers.

The experimental fuzz harness used to find these cases is not included.

AArnott and others added 5 commits October 18, 2026 22:18
IsFirstInLine and IsLastInLine checked SyntaxTree for null after calling
GetLineSpan, so a postfix operator at the end of an incomplete buffer
crashed SA1003. Check for a missing tree first.

The new helper tests fail on master with the NullReferenceException at
TokenHelper.cs. The incomplete b++ analyzer test locks the editor case.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
The code fix replaced only the declared name. A use such as value.nameA
then failed to compile. Tuple element symbols cannot go through Renamer,
so the declaration and its references are replaced directly. Tuple literal
names still have no symbol and keep the token-only replacement.

The new test fails on master with the declaration renamed and the use left
unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
Stripping the underscore from _testEvent produced testEvent, which was
already the event name, so the fix introduced CS0102. The code fix is now
withheld when the resulting name is not a valid member name.

The new test fails on master by showing the field and its uses renamed onto
the existing event.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
SA1642, SA1643, and SA1600 built standard text from the containing type
without checking that one exists. A finalizer in a loose block therefore
threw NullReferenceException from GetTypeParameterList. The fix is not
offered when there is no type to name.

Both new tests fail on master with that exception.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
After the null-tree crash is fixed, Roslyn 3.8 and later still see the
missing token after b++ and report that ++ should be followed by space.
Earlier compilers do not. The test now matches that split, and the code
fix inserts the space.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
@AArnott
AArnott requested a balanced review from Copilot October 19, 2026 04:26
@AArnott
AArnott marked this pull request as ready for review October 19, 2026 04:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The tuple-reference rewrite can corrupt documentation-adjacent tokens and break inferred anonymous-property names.

2 open issues
What changed in this PR

Addresses analyzer crashes and unsafe code fixes discovered through syntax fuzzing, without adding the fuzzing harness.

Changes:

  • Guards token helpers against missing syntax trees.
  • Updates tuple references and blocks conflicting field renames.
  • Suppresses documentation fixes without a containing type and adds regression tests.
File Description
test/​StyleCop.Analyzers.Test/​SpacingRules/​SA1003UnitTests.cs Tests incomplete postfix increments.
test/​StyleCop.Analyzers.Test/​NamingRules/​SA1309UnitTests.cs Tests conflicting field renames.
test/​StyleCop.Analyzers.Test/​HelperTests/​TokenHelperTests.cs Tests tokens without syntax trees.
test/​StyleCop.Analyzers.Test/​DocumentationRules/​SA1643UnitTests.cs Tests finalizers outside types.
test/​StyleCop.Analyzers.Test/​DocumentationRules/​SA1600UnitTests.cs Tests documentation-fix suppression.
test/​StyleCop.Analyzers.Test.CSharp7/​NamingRules/​SA1316CSharp7UnitTests.cs Tests tuple-reference updates.
src/​StyleCop.Analyzers/​Helpers/​TokenHelper.cs Checks for null trees before accessing spans.
src/​StyleCop.Analyzers.CodeFixes/​NamingRules/​SA1316CodeFixProvider.cs Rewrites tuple declarations and references.
src/​StyleCop.Analyzers.CodeFixes/​NamingRules/​SA1309CodeFixProvider.cs Rejects conflicting rename targets.
src/​StyleCop.Analyzers.CodeFixes/​DocumentationRules/​SA1642SA1643CodeFixProvider.cs Guards missing containing types.
src/​StyleCop.Analyzers.CodeFixes/​DocumentationRules/​SA1600CodeFixProvider.cs Suppresses fixes without containing types.

Review effort: Balanced

@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.27517% with 65 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.65%. Comparing base (013effd) to head (03884e7).
⚠️ Report is 11 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4344      +/-   ##
==========================================
- Coverage   97.68%   97.65%   -0.04%     
==========================================
  Files        1333     1334       +1     
  Lines      148066   148794     +728     
  Branches     4998     5052      +54     
==========================================
+ Hits       144634   145299     +665     
- Misses       2172     2215      +43     
- Partials     1260     1280      +20     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

FindToken without searching trivia returns the token that owns a documentation
comment, so a cref to a tuple element replaced the class keyword and threw
ArgumentException. Search trivia and replace only a token that starts at the
reference.

Renaming the identifier in an inferred anonymous projection also changed the
property name, so later uses no longer compiled and an explicit property of the
corrected name became a duplicate. Preserve the original inferred name as an
explicit initializer before renaming the remaining references.

Both regressions fail on the previous commit: the cref case throws from
ClassDeclarationSyntax.Update, and the anonymous cases produce value.NameA
without keeping the original property name.

The analyzer is unchanged. The extra work is in the code fix and runs only
when a fix is requested.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
@AArnott
AArnott marked this pull request as draft October 19, 2026 05:32
@AArnott
AArnott requested a balanced review from Copilot October 19, 2026 05:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

SA1316 can still generate invalid code for escaped identifiers and inferred tuple projections.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Preserve inferred tuple names in projected references

src/​StyleCop.Analyzers.CodeFixes/​NamingRules/​SA1316CodeFixProvider.cs:145

Inferred tuple projections also need name preservation. In C# 7.1+, with (int nameA, int Other) value, var projected = (value.nameA, 0L); return projected.nameA; becomes (value.NameA, 0L) while the final access stays unchanged and no longer compiles. The projected field belongs to a different tuple type, so references to the original field do not include that consumer. Preserve the inferred name explicitly (nameA: value.NameA) before replacing the reference, and add a regression test.

2 resolved since last review

Review effort: Balanced

ValueText drops the '@' from an escaped identifier, so renaming a tuple
element in `new { value.@Class }` produced `class = value.Class`, which
does not compile. Copy the original identifier token without trivia or
the rename annotation.

The new test fails on the previous commit by showing `class = value.Class`.

The analyzer is unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
@AArnott
AArnott marked this pull request as ready for review October 19, 2026 05:45
Copilot AI balanced review requested due to automatic review settings October 19, 2026 05:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

SA1316 still risks breaking inferred tuple projections and throwing when rewriting Visual Basic references.

2 open issues
1 resolved since last review

Review effort: Balanced

Renaming a tuple element used as an inferred tuple name changed that name.
When the corrected name was free, the projection and its consumers now use
the corrected explicit name. When it would select another element, the fix
is not offered.

A Visual Basic reference cannot be rewritten with a C# token. Skipping it
would leave the reference broken, so the fix is not offered unless every
reference is in a C# document.

The inferred-name regressions fail on the previous commit by producing
value.NameA and leaving projected.nameA unchanged. The collision and Visual
Basic cases would still offer a fix that cannot be applied safely.

The analyzer is unchanged. The extra checks run only when a fix is requested.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
@AArnott
AArnott marked this pull request as draft October 19, 2026 06:13
Microsoft.CodeAnalysis.VisualBasic.Workspaces 2.8.2 flowed through the C# 7
test project into later test projects, which resolve a newer Workspaces.Common.
That mismatch is NU1608 and failed the solution build. The package is only
needed to compile the mixed-language test, so it is private to that project.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
@AArnott
AArnott marked this pull request as ready for review October 19, 2026 13:55
Copilot AI balanced review requested due to automatic review settings October 19, 2026 13:55

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

SA1316 can still generate invalid code for chained projections and duplicate inferred tuple names.

2 open issues
2 resolved since last review

Review effort: Balanced

Consumer discovery stopped after the first inferred projection, so a second
projection was renamed while its own consumers were left unchanged. Newly
discovered consumers are now walked, including collision and language checks.

Duplicate inferred names are dropped by the compiler, so both elements remain
Item1 and Item2. The fix is not offered when the resolved element name is not
the inferred identifier.

Both regressions fail on the previous commit: the chain leaves second.nameA
unchanged, and the duplicate case still offers a fix.

The analyzer is unchanged.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7b84c03c-f9e3-4447-96f6-caff454f3096
@AArnott
AArnott marked this pull request as draft October 19, 2026 14:12
@AArnott
AArnott marked this pull request as ready for review October 19, 2026 14:16
This was referenced Nov 23, 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