Reject negative char index in the length-and-char string switch#3881
Merged
Conversation
MatchSwitchOnCharBlock's case 2 and default paths guarded against a negative character index, but case 1 (a bare switch on get_Chars) did not. Crafted IL whose get_Chars/get_Item index is negative - a value no compiler emits, but valid IL - therefore reached the pattern reconstruction unchecked. For a length-1 group this silently miscompiled the switch (it rebuilds the string switch from the char labels without using the index), turning IL that reads s[-1] into `switch (s)`; for longer strings it threw IndexOutOfRangeException and aborted the method. Move the check into MatchGetChars so all three call sites reject a negative index by construction, and drop the two now-redundant guards. Same class of unvalidated-integer robustness issue as #3878, in a different switch-on-string pattern. Assisted-by: Claude:claude-opus-4-8:Claude Code
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the string-switch transform for the Roslyn “switch on length and char” pattern by rejecting negative get_Chars / Span.get_Item indices at match time, preventing both silent miscompilation (length==1 path) and decompilation failures (longer strings).
Changes:
- Enforce
index >= 0insideMatchGetCharsfor bothSystem.String.get_CharsandReadOnlySpan/Span.get_Item. - Remove now-redundant negative-index guards from
MatchSwitchOnCharBlockcases that callMatchGetChars. - Add an ILPretty regression fixture + test that edits the
get_Charsindex to-1and verifies the transform declines and preservesswitch (s[-1]).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| ICSharpCode.Decompiler/IL/Transforms/SwitchOnStringTransform.cs | Centralizes non-negative index validation in MatchGetChars and removes redundant per-case checks. |
| ICSharpCode.Decompiler.Tests/TestCases/ILPretty/SwitchOnStringNegativeCharIndex.il | Adds a regression IL fixture that uses a negative get_Chars index to exercise the bug. |
| ICSharpCode.Decompiler.Tests/TestCases/ILPretty/SwitchOnStringNegativeCharIndex.cs | Adds the expected decompiler output, ensuring the transform declines and preserves switch (s[-1]). |
| ICSharpCode.Decompiler.Tests/ILPrettyTestRunner.cs | Registers the new ILPretty test case in the test runner. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Follow-up to #3878, which fixed a negative-dictionary-capacity crash in the string-switch transform. The same class of bug — an unvalidated negative integer read from IL — exists in a different switch-on-string pattern: the Roslyn 4.6 "switch on length and char" reconstruction.
In
MatchSwitchOnCharBlock,case 2anddefaultguard the character index withif (index < 0) return false;, butcase 1(a bareswitchonget_Chars) does not. IL whoseget_Chars/get_Itemindex is negative — e.g.s[-1], a value no compiler emits in this pattern but perfectly valid IL — reaches the reconstruction unchecked:index; it rebuilds the string switch from the char labels alone, so IL that readss[-1]is silently miscompiled intoswitch (s)(semantically wrong — the IL throws on any 1-char string, the output does not).stringValue[index]throwsIndexOutOfRangeException, aborting decompilation of the whole method.Fix
Move the non-negative check into
MatchGetChars(both theget_CharsandReadOnlySpan/Span.get_Itembranches), so all three call sites reject a negative index by construction, and drop the two now-redundant per-case guards.Test
ILPretty/SwitchOnStringNegativeCharIndex— real Roslyn length-1switch-on-string codegen with a single instruction edited (ldc.i4.0→ldc.i4.m1). Without the fix the expected output diff shows the miscompiledreturn s switch { … }; with it, the decompiler correctly declines and preservesswitch (s[-1]).🤖 Generated with Claude Code