Skip to content

Commit 5912df3

Browse files
committed
Explicitly handle empty paths
1 parent ed19978 commit 5912df3

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

Sources/TSCBasic/Path.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,12 @@ private struct UNIXPath: Path {
456456
defer { fsr.deallocate() }
457457

458458
let path: String = String(cString: fsr)
459-
return path.withCString(encodedAs: UTF16.self) {
459+
let result: String = path.withCString(encodedAs: UTF16.self) {
460460
let data = UnsafeMutablePointer(mutating: $0)
461461
PathCchRemoveFileSpec(data, path.count)
462462
return String(decodingCString: data, as: UTF16.self)
463463
}
464+
return result.isEmpty ? "." : result
464465
#else
465466
// FIXME: This method seems too complicated; it should be simplified,
466467
// if possible, and certainly optimized (using UTF8View).
@@ -692,6 +693,12 @@ private struct UNIXPath: Path {
692693

693694
init(validatingAbsolutePath path: String) throws {
694695
#if os(Windows)
696+
// Explicitly handle the empty path, since retrieving
697+
// `fileSystemRepresentation` of it is illegal.
698+
guard !path.isEmpty else {
699+
throw PathValidationError.invalidAbsolutePath(path)
700+
}
701+
695702
let fsr: UnsafePointer<Int8> = path.fileSystemRepresentation
696703
defer { fsr.deallocate() }
697704

@@ -714,6 +721,12 @@ private struct UNIXPath: Path {
714721

715722
init(validatingRelativePath path: String) throws {
716723
#if os(Windows)
724+
// Explicitly handle the empty path, since retrieving
725+
// `fileSystemRepresentation` of it is illegal.
726+
guard !path.isEmpty else {
727+
throw PathValidationError.invalidRelativePath(path)
728+
}
729+
717730
let fsr: UnsafePointer<Int8> = path.fileSystemRepresentation
718731
defer { fsr.deallocate() }
719732

0 commit comments

Comments
 (0)