Skip to content

Commit

Permalink
TSCBasic: correct a use-after-free on Windows (swiftlang#232)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
compnerd authored Jun 28, 2021
1 parent 4c4ad66 commit c5dd301
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions Sources/TSCBasic/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -730,13 +730,14 @@ private struct UNIXPath: Path {

func suffix(withDot: Bool) -> String? {
#if os(Windows)
let ext = self.string.withCString(encodedAs: UTF16.self) {
PathFindExtensionW($0)
return self.string.withCString(encodedAs: UTF16.self) {
if let pointer = PathFindExtensionW($0) {
let substring = String(decodingCString: pointer, as: UTF16.self)
guard substring.length > 0 else { return nil }
return withDot ? substring : String(substring.dropFirst(1))
}
return nil
}
var result = String(decodingCString: ext!, as: UTF16.self)
guard result.length > 0 else { return nil }
if !withDot { result.removeFirst(1) }
return result
#else
// FIXME: This method seems too complicated; it should be simplified,
// if possible, and certainly optimized (using UTF8View).
Expand Down

0 comments on commit c5dd301

Please sign in to comment.