Skip to content

Commit c57eb1c

Browse files
Fix out of bounds in text extent computation (#77402)
2 parents 008b61f + 0e4da9f commit c57eb1c

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/EditorFeatures/CSharp/TextStructureNavigation/CSharpTextStructureNavigatorProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected override TextExtent GetExtentOfWordFromToken(ITextStructureNavigator n
9797
contentStart++;
9898
}
9999

100-
var end = token.Span.End;
100+
var end = Math.Max(contentStart, token.Span.End);
101101
var contentEnd = end;
102102

103103
if (CharAt(contentEnd - 1) == '8')
@@ -115,6 +115,8 @@ protected override TextExtent GetExtentOfWordFromToken(ITextStructureNavigator n
115115
contentEnd--;
116116
}
117117

118+
// Ensure that in error conditions like a naked `"` that we don't end up with invalid bounds.
119+
contentEnd = Math.Max(contentStart, contentEnd);
118120
return (TextSpan.FromBounds(start, contentStart), TextSpan.FromBounds(contentStart, contentEnd), TextSpan.FromBounds(contentEnd, end));
119121
}
120122

src/EditorFeatures/CSharpTest/TextStructureNavigation/TextStructureNavigatorTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,4 +561,13 @@ public void TestClampStringLiteral(string content)
561561
{
562562
AssertExtent(content);
563563
}
564+
565+
[WpfTheory, WorkItem("https://github.com/dotnet/roslyn/issues/77401")]
566+
[InlineData(@"{|Significant:""|}$$")]
567+
[InlineData(@"{|Significant:""""""|}$$")]
568+
[InlineData(@"""""""{|Significant:u8|}$$")]
569+
public void TestClampStringLiteral_Invalid(string content)
570+
{
571+
AssertExtent(content);
572+
}
564573
}

0 commit comments

Comments
 (0)