Repository navigation
Fix preview crash on UNC and malformed paths #2052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Henglie
wants to merge
3
commits into
QL-Win:master
Choose a base branch
from
Henglie:fix/unc-path-preview
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| // Copyright © 2017-2026 QL-Win Contributors | ||
| // | ||
| // This file is part of QuickLook program. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace QuickLook.Common.Helpers; | ||
|
|
||
| public static class PathHelper | ||
| { | ||
| private static readonly char[] TrimmableExtensionChars = ['"', ' ', '\t', '\r', '\n']; | ||
|
|
||
| public static string GetExtension(string path) | ||
| { | ||
| if (string.IsNullOrEmpty(path)) | ||
| return string.Empty; | ||
|
|
||
| var normalized = Normalize(path); | ||
|
|
||
| // Path.GetExtension validates the whole path through Path.CheckInvalidPathChars | ||
| // and can throw for shell-provided paths. Parse UNC paths directly so they | ||
| // always reach the preview pipeline. | ||
| if (IsUncPath(normalized)) | ||
| return GetExtensionFallback(normalized); | ||
|
|
||
| try | ||
| { | ||
| return Path.GetExtension(normalized); | ||
| } | ||
| catch (ArgumentException) | ||
| { | ||
| return GetExtensionFallback(normalized); | ||
| } | ||
| } | ||
|
|
||
| public static string GetFileName(string path) | ||
| { | ||
| if (string.IsNullOrEmpty(path)) | ||
| return path; | ||
|
|
||
| var normalized = Normalize(path); | ||
|
|
||
| try | ||
| { | ||
| return Path.GetFileName(normalized); | ||
| } | ||
| catch (ArgumentException) | ||
| { | ||
| var separator = normalized.LastIndexOfAny([Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar]); | ||
| return separator < 0 ? normalized : normalized.Substring(separator + 1); | ||
| } | ||
| } | ||
|
|
||
| public static string GetPathRoot(string path) | ||
| { | ||
| if (string.IsNullOrEmpty(path)) | ||
| return string.Empty; | ||
|
|
||
| var normalized = Normalize(path); | ||
|
|
||
| try | ||
| { | ||
| return Path.GetPathRoot(normalized); | ||
| } | ||
| catch (ArgumentException) | ||
| { | ||
| if (normalized.StartsWith(@"\\", StringComparison.Ordinal)) | ||
| { | ||
| var parts = normalized.Substring(2).Split([Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar], 3); | ||
| return parts.Length switch | ||
| { | ||
| 0 => string.Empty, | ||
| 1 => @"\\" + parts[0], | ||
| _ => @"\\" + parts[0] + @"\" + parts[1], | ||
| }; | ||
| } | ||
|
|
||
| if (normalized.Length >= 2 && normalized[1] == Path.VolumeSeparatorChar) | ||
| return normalized.Substring(0, 2) + Path.DirectorySeparatorChar; | ||
|
|
||
| return string.Empty; | ||
| } | ||
| } | ||
|
|
||
| public static DateTime? GetLastWriteTime(string path) | ||
| { | ||
| try | ||
| { | ||
| var last = File.GetLastWriteTime(path); | ||
| if (last.Year == 1601) | ||
| { | ||
| // File.GetLastWriteTime returns the 1601 sentinel for missing or unreadable paths. | ||
| return null; | ||
| } | ||
|
|
||
| return last; | ||
| } | ||
| catch (Exception e) when (e is ArgumentException || | ||
| e is UnauthorizedAccessException || | ||
| e is IOException || | ||
| e is NotSupportedException) | ||
| { | ||
| // An unusable path must not hide the rest of the info card. | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public static bool IsShortcut(string path) | ||
| { | ||
| return GetExtension(path).Equals(".lnk", StringComparison.OrdinalIgnoreCase); | ||
| } | ||
|
|
||
| public static bool IsUncPath(string path) | ||
| { | ||
| if (string.IsNullOrEmpty(path)) | ||
| return false; | ||
|
|
||
| var normalized = Normalize(path); | ||
| return normalized.Length >= 2 && | ||
| normalized[0] == Path.DirectorySeparatorChar && | ||
| normalized[1] == Path.DirectorySeparatorChar; | ||
| } | ||
|
|
||
| private static string Normalize(string path) | ||
| { | ||
| path = path.Trim(); | ||
|
|
||
| if (path.Length >= 2 && path[0] == '"' && path[path.Length - 1] == '"') | ||
| path = path.Substring(1, path.Length - 2).Trim(); | ||
|
|
||
| return path; | ||
| } | ||
|
|
||
| private static string GetExtensionFallback(string path) | ||
| { | ||
| var end = path.Length - 1; | ||
|
|
||
| while (end >= 0 && char.IsWhiteSpace(path[end])) | ||
| end--; | ||
|
|
||
| for (var i = end; i >= 0; i--) | ||
| { | ||
| var c = path[i]; | ||
|
|
||
| if (c == '.') | ||
| return i == end | ||
| ? string.Empty | ||
| : path.Substring(i, end - i + 1).TrimEnd(TrimmableExtensionChars); | ||
|
|
||
| if (c == Path.DirectorySeparatorChar || | ||
| c == Path.AltDirectorySeparatorChar || | ||
| c == Path.VolumeSeparatorChar) | ||
| break; | ||
| } | ||
|
|
||
| return string.Empty; | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Copyright © 2017-2026 QL-Win Contributors | ||
| // | ||
| // This file is part of QuickLook program. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using QuickLook.Common.Helpers; | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace QuickLook.Tests; | ||
|
|
||
| [TestClass] | ||
| public sealed class PathHelperTests | ||
| { | ||
| [TestMethod] | ||
| [DataRow(@"\\server\share\file.png", ".png")] | ||
| [DataRow(@"\\server.domain\share\folder\image.JPG", ".JPG")] | ||
| [DataRow(@"\\10.1.2.3\share\file.pdf", ".pdf")] | ||
| [DataRow(@"\\wsl$\Ubuntu\home\user\file.png", ".png")] | ||
| [DataRow(@"\\wsl.localhost\Ubuntu\home\user\file.png", ".png")] | ||
| [DataRow(@"\\server\share\folder", "")] | ||
| [DataRow(@"\\wsl$\Ubuntu\home\user", "")] | ||
| [DataRow(@"C:\local\folder\file.png", ".png")] | ||
| [DataRow(@"Z:\mapped\folder\file.png", ".png")] | ||
| [DataRow(@"C:\local\folder\file", "")] | ||
| [DataRow("", "")] | ||
| public void GetExtensionHandlesUncAndLocalPaths(string path, string expected) | ||
| { | ||
| Assert.AreEqual(expected, PathHelper.GetExtension(path)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DataRow(@"\\server\share\file.png" + "\n")] | ||
| [DataRow(@"\\server\share\bad""name.png")] | ||
| [DataRow(@"""\\server\share\file.png""")] | ||
| public void GetExtensionDoesNotThrowOnMalformedUncPaths(string path) | ||
| { | ||
| Assert.AreEqual(".png", PathHelper.GetExtension(path)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DataRow(@"\\server\share\bad""name.png")] | ||
| [DataRow(@"""\\server\share\file.png""")] | ||
| [DataRow("")] | ||
| public void GetLastWriteTimeReturnsNullForMalformedPaths(string path) | ||
| { | ||
| Assert.IsNull(PathHelper.GetLastWriteTime(path)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetLastWriteTimeReturnsNullForUnsupportedPathFormat() | ||
| { | ||
| Assert.IsNull(PathHelper.GetLastWriteTime(@"C::\file.png")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetLastWriteTimeReturnsNullForMissingPath() | ||
| { | ||
| var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".missing"); | ||
| Assert.IsNull(PathHelper.GetLastWriteTime(path)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void GetLastWriteTimeReturnsValueForExistingFile() | ||
| { | ||
| var path = Path.Combine(Path.GetTempPath(), Guid.NewGuid() + ".tmp"); | ||
| File.WriteAllText(path, string.Empty); | ||
|
|
||
| try | ||
| { | ||
| Assert.IsTrue(PathHelper.GetLastWriteTime(path).HasValue); | ||
| } | ||
| finally | ||
| { | ||
| File.Delete(path); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DataRow(@"\\server\share\file.lnk", true)] | ||
| [DataRow(@"\\server\share\file.LNK", true)] | ||
| [DataRow(@"\\wsl$\Ubuntu\home\file.png", false)] | ||
| [DataRow(@"C:\local\folder\file.lnk", true)] | ||
| [DataRow(@"C:\local\folder\file.png", false)] | ||
| [DataRow(@"\\wsl$\Ubuntu\home", false)] | ||
| public void IsShortcutHandlesUncAndLocalPaths(string path, bool expected) | ||
| { | ||
| Assert.AreEqual(expected, PathHelper.IsShortcut(path)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DataRow(@"\\server\share\file.png", true)] | ||
| [DataRow(@"\\wsl$\Ubuntu\home\file.png", true)] | ||
| [DataRow(@"""\\server\share\file.png""", true)] | ||
| [DataRow(@"C:\local\folder\file.png", false)] | ||
| [DataRow(@"Z:\mapped\file.png", false)] | ||
| [DataRow(@"\server\share\file.png", false)] | ||
| [DataRow("", false)] | ||
| public void IsUncPathHandlesUncAndLocalPaths(string path, bool expected) | ||
| { | ||
| Assert.AreEqual(expected, PathHelper.IsUncPath(path)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DataRow(@"\\server\share\folder\file.png", "file.png")] | ||
| [DataRow(@"\\wsl$\Ubuntu\home\user\file.png", "file.png")] | ||
| [DataRow(@"C:\local\folder\file.png", "file.png")] | ||
| [DataRow(@"\\server\share\bad""name.png", "bad\"name.png")] | ||
| public void GetFileNameHandlesUncAndMalformedPaths(string path, string expected) | ||
| { | ||
| Assert.AreEqual(expected, PathHelper.GetFileName(path)); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DataRow(@"\\server\share\folder\file.png", @"\\server\share")] | ||
| [DataRow(@"\\wsl$\Ubuntu\home\user\file.png", @"\\wsl$\Ubuntu")] | ||
| [DataRow(@"C:\local\folder\file.png", @"C:\")] | ||
| [DataRow(@"Z:\mapped\file.png", @"Z:\")] | ||
| public void GetPathRootHandlesUncAndLocalPaths(string path, string expected) | ||
| { | ||
| Assert.AreEqual(expected, PathHelper.GetPathRoot(path)); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net462</TargetFramework> | ||
| <IsPackable>false</IsPackable> | ||
| <LangVersion>latest</LangVersion> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> | ||
| <PackageReference Include="MSTest.TestAdapter" Version="3.6.4" /> | ||
| <PackageReference Include="MSTest.TestFramework" Version="3.6.4" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="..\QuickLook.Common\Helpers\PathHelper.cs" Link="PathHelper.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check the UTC sentinel before converting to local time.
File.GetLastWriteTimeconverts the timestamp to local time. In a negative UTC offset, the missing-file sentinel becomes December 31, 1600. Thelast.Year == 1601check therefore misses it. The .NET Framework implementation performs this local-time conversion explicitly. (raw.githubusercontent.com)This makes
GetLastWriteTimeReturnsNullForMissingPathfail on affected Windows machines. It also lets the info panel display the sentinel as a real modification date.Read the UTC timestamp, compare the exact sentinel, then convert valid timestamps to local time.
Proposed fix