Skip to content

Commit 5d31e72

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 5d31e72

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Sources/TSCBasic/Path.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,18 @@ public struct RelativePath: Hashable {
254254
/// normalization or canonicalization. This will construct a path without
255255
/// an anchor and thus may be invalid.
256256
fileprivate init(unsafeUncheckedPath string: String) {
257-
self.init(PathImpl(string: string))
257+
if string.isEmpty {
258+
self.init(PathImpl(string: string))
259+
} else {
260+
#if _runtime(_ObjC)
261+
self.init(PathImpl(string: string.fileSystemRepresentation))
262+
#else
263+
let normalized: UnsafePointer<Int8> = string.fileSystemRepresentation
264+
defer { normalized.deallocate() }
265+
266+
self.init(PathImpl(string: String(cString: normalized)))
267+
#endif
268+
}
258269
}
259270

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

0 commit comments

Comments
 (0)