Skip to content

Commit 20a6de6

Browse files
committed
TSCBasic: correct a use-after-free on Windows (swiftlang#232)
This corrects a use-after-free caused by a pointer escape. The lifetime of the pointer is not guaranteed, and the OS may free the pointer early, particularly with OSSA. Push the handling into the closure to ensure that the lifetime is extended for the duration.
1 parent e95add8 commit 20a6de6

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

Sources/TSCBasic/Path.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -727,13 +727,14 @@ private struct UNIXPath: Path {
727727

728728
func suffix(withDot: Bool) -> String? {
729729
#if os(Windows)
730-
let ext = self.string.withCString(encodedAs: UTF16.self) {
731-
PathFindExtensionW($0)
730+
return self.string.withCString(encodedAs: UTF16.self) {
731+
if let pointer = PathFindExtensionW($0) {
732+
let substring = String(decodingCString: pointer, as: UTF16.self)
733+
guard substring.length > 0 else { return nil }
734+
return withDot ? substring : String(substring.dropFirst(1))
735+
}
736+
return nil
732737
}
733-
var result = String(decodingCString: ext!, as: UTF16.self)
734-
guard result.length > 0 else { return nil }
735-
if !withDot { result.removeFirst(1) }
736-
return result
737738
#else
738739
// FIXME: This method seems too complicated; it should be simplified,
739740
// if possible, and certainly optimized (using UTF8View).

0 commit comments

Comments
 (0)