Skip to content

Commit c5dd301

Browse files
authored
TSCBasic: correct a use-after-free on Windows (#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 4c4ad66 commit c5dd301

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
@@ -730,13 +730,14 @@ private struct UNIXPath: Path {
730730

731731
func suffix(withDot: Bool) -> String? {
732732
#if os(Windows)
733-
let ext = self.string.withCString(encodedAs: UTF16.self) {
734-
PathFindExtensionW($0)
733+
return self.string.withCString(encodedAs: UTF16.self) {
734+
if let pointer = PathFindExtensionW($0) {
735+
let substring = String(decodingCString: pointer, as: UTF16.self)
736+
guard substring.length > 0 else { return nil }
737+
return withDot ? substring : String(substring.dropFirst(1))
738+
}
739+
return nil
735740
}
736-
var result = String(decodingCString: ext!, as: UTF16.self)
737-
guard result.length > 0 else { return nil }
738-
if !withDot { result.removeFirst(1) }
739-
return result
740741
#else
741742
// FIXME: This method seems too complicated; it should be simplified,
742743
// if possible, and certainly optimized (using UTF8View).

0 commit comments

Comments
 (0)