Skip to content

Commit d7bd437

Browse files
authored
Merge pull request #242 from compnerd/5.4-uaf
TSCBasic: correct a use-after-free on Windows (#232)
2 parents e95add8 + 20a6de6 commit d7bd437

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)