Skip to content

Commit 8de0938

Browse files
Fix pattern matching to avoid IndexOutOfRangeException (#9357)
This fixes a problem introduce late into the file path normalization pull request.
2 parents d9b92ee + 49e76e7 commit 8de0938

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/Utilities/FilePathNormalizer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static string NormalizeDirectory(string? directoryFilePath)
2828
ReadOnlySpan<char> normalizedSpan = arraySpan.Slice(start, length);
2929

3030
// Add a trailing slash if the normalized span doesn't end in one.
31-
if (normalizedSpan[^1] != '/')
31+
if (normalizedSpan is not [.., '/'])
3232
{
3333
arraySpan[start + length] = '/';
3434
normalizedSpan = arraySpan.Slice(start, length + 1);
@@ -169,7 +169,7 @@ private static (int start, int length) NormalizeCore(ReadOnlySpan<char> source,
169169
destination[..charsWritten].Replace('\\', '/');
170170

171171
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) &&
172-
destination is ['/', not '/', ..])
172+
destination is ['/', ..] and not ['/', '/', ..])
173173
{
174174
// We've been provided a path that probably looks something like /C:/path/to.
175175
// So, we adjust the result to skip the leading '/'.

src/Razor/test/Microsoft.AspNetCore.Razor.ProjectEngineHost.Test/FilePathNormalizerTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ public void NormalizeDirectory_EndsWithoutSlash()
6262
Assert.Equal("C:/path/to/directory/", normalized);
6363
}
6464

65+
[OSSkipConditionFact(new[] { "OSX", "Linux" })]
66+
public void NormalizeDirectory_Windows_HandlesSingleSlashDirectory()
67+
{
68+
// Arrange
69+
var directory = @"\";
70+
71+
// Act
72+
var normalized = FilePathNormalizer.NormalizeDirectory(directory);
73+
74+
// Assert
75+
Assert.Equal("/", normalized);
76+
}
77+
6578
[Fact]
6679
public void FilePathsEquivalent_NotEqualPaths_ReturnsFalse()
6780
{

0 commit comments

Comments
 (0)