Skip to content

Commit

Permalink
Explicitly handle empty paths
Browse files Browse the repository at this point in the history
  • Loading branch information
stevapple committed Dec 7, 2021
1 parent ed19978 commit 5912df3
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Sources/TSCBasic/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,12 @@ private struct UNIXPath: Path {
defer { fsr.deallocate() }

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

init(validatingAbsolutePath path: String) throws {
#if os(Windows)
// Explicitly handle the empty path, since retrieving
// `fileSystemRepresentation` of it is illegal.
guard !path.isEmpty else {
throw PathValidationError.invalidAbsolutePath(path)
}

let fsr: UnsafePointer<Int8> = path.fileSystemRepresentation
defer { fsr.deallocate() }

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

init(validatingRelativePath path: String) throws {
#if os(Windows)
// Explicitly handle the empty path, since retrieving
// `fileSystemRepresentation` of it is illegal.
guard !path.isEmpty else {
throw PathValidationError.invalidRelativePath(path)
}

let fsr: UnsafePointer<Int8> = path.fileSystemRepresentation
defer { fsr.deallocate() }

Expand Down

0 comments on commit 5912df3

Please sign in to comment.