Skip to content

Commit 561bd3d

Browse files
committed
TSCBasic: protect against an invalid API usage
`filePathRepresentation` may not be invoked on an empty string. This protects against that case which does occur during the SPM test suite execution.
1 parent 0d37c46 commit 561bd3d

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

Sources/TSCBasic/Path.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ private typealias PathImpl = UNIXPath
1818
private typealias PathImpl = UNIXPath
1919
#endif
2020

21+
#if canImport(Darwin)
22+
import Darwin.C
23+
#endif
24+
25+
import class Foundation.NSURL
2126
import protocol Foundation.CustomNSError
2227
import var Foundation.NSLocalizedDescriptionKey
2328

@@ -254,7 +259,18 @@ public struct RelativePath: Hashable {
254259
/// normalization or canonicalization. This will construct a path without
255260
/// an anchor and thus may be invalid.
256261
fileprivate init(unsafeUncheckedPath string: String) {
257-
self.init(PathImpl(string: string))
262+
if string.isEmpty {
263+
self.init(PathImpl(string: string))
264+
} else {
265+
#if _runtime(_ObjC)
266+
self.init(PathImpl(string: String(cString: NSURL(fileURLWithPath: string).fileSystemRepresentation)))
267+
#else
268+
let normalized: UnsafePointer<Int8> = string.fileSystemRepresentation
269+
defer { normalized.deallocate() }
270+
271+
self.init(PathImpl(string: String(cString: normalized)))
272+
#endif
273+
}
258274
}
259275

260276
/// Initializes the RelativePath from `str`, which must be a relative path

0 commit comments

Comments
 (0)