Skip to content

Commit 1818bf7

Browse files
committed
Avoid exception in ScriptFile.IsUntitledPath()
Since we could theoretically receive any complicated string.
1 parent 5f08df6 commit 1818bf7

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/PowerShellEditorServices/Services/TextDocument/ScriptFile.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,18 +195,17 @@ internal static List<string> GetLinesInternal(string text)
195195
}
196196

197197
/// <summary>
198-
/// Deterines whether the supplied path indicates the file is an "untitled:Unitled-X"
198+
/// Determines whether the supplied path indicates the file is an "untitled:Untitled-X"
199199
/// which has not been saved to file.
200200
/// </summary>
201201
/// <param name="path">The path to check.</param>
202202
/// <returns>True if the path is an untitled file, false otherwise.</returns>
203203
internal static bool IsUntitledPath(string path)
204204
{
205205
Validate.IsNotNull(nameof(path), path);
206-
return !string.Equals(
207-
DocumentUri.From(path).Scheme,
208-
Uri.UriSchemeFile,
209-
StringComparison.OrdinalIgnoreCase);
206+
// This may not have been given a URI, so return false instead of throwing.
207+
return Uri.IsWellFormedUriString(path, UriKind.RelativeOrAbsolute) &&
208+
!string.Equals(DocumentUri.From(path).Scheme, Uri.UriSchemeFile, StringComparison.OrdinalIgnoreCase);
210209
}
211210

212211
/// <summary>

test/PowerShellEditorServices.Test/Session/ScriptFileTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ public void DocumentUriReturnsCorrectStringForAbsolutePath()
651651
[InlineData("vscode-notebook-cell:/Users/me/Documents/test.ps1#0001", true)]
652652
[InlineData("https://microsoft.com", true)]
653653
[InlineData("Untitled:Untitled-1", true)]
654+
[InlineData("'a log statement' > 'c:\\Users\\me\\Documents\\test.txt'\r\n", false)]
654655
public void IsUntitledFileIsCorrect(string path, bool expected) => Assert.Equal(expected, ScriptFile.IsUntitledPath(path));
655656
}
656657
}

0 commit comments

Comments
 (0)