Skip to content

Commit 0c683b6

Browse files
committed
Conditionalize stripping of \\?\ on it actually being present
As far as I know `\\?\` is always returned by `GetFinalPathNameByHandleW` but it’s better to be safe and actually check instead of unconditionally trimming the first 4 characters from the returned path. See swiftlang/swift-tools-support-core#485 (comment)
1 parent 33a49e5 commit 0c683b6

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

Sources/FoundationEssentials/String/String+Path.swift

+8-2
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,14 @@ extension String {
722722
return nil
723723
}
724724

725-
// When using `VOLUME_NAME_DOS`, the returned path uses `\\?\`.
726-
return String(decodingCString: $0.baseAddress!.advanced(by: 4), as: UTF16.self)
725+
let pathBaseAddress: UnsafePointer<WCHAR>
726+
if Array($0.prefix(4)) == Array(#"\\?\"#.utf16) {
727+
// When using `VOLUME_NAME_DOS`, the returned path uses `\\?\`.
728+
pathBaseAddress = UnsafePointer($0.baseAddress!.advanced(by: 4))
729+
} else {
730+
pathBaseAddress = UnsafePointer($0.baseAddress!)
731+
}
732+
return try AbsolutePath(String(decodingCString: pathBaseAddress, as: UTF16.self))
727733
}
728734
}
729735
#else // os(Windows)

0 commit comments

Comments
 (0)