Skip to content

TSCBasic: handle empty paths in WindowsPath(validatingRelativePath:) #403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions Sources/TSCBasic/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -527,12 +527,15 @@ private struct WindowsPath: Path, Sendable {
self.string = string
}

init(normalizingAbsolutePath path: String) {
let normalized: UnsafePointer<Int8> = path.fileSystemRepresentation
defer { normalized.deallocate() }
private static func repr(_ path: String) -> String {
guard !path.isEmpty else { return "" }
let representation: UnsafePointer<Int8> = path.fileSystemRepresentation
defer { representation.deallocate() }
return String(cString: representation)
}

self.init(string: String(cString: normalized)
.withCString(encodedAs: UTF16.self) { pwszPath in
init(normalizingAbsolutePath path: String) {
self.init(string: Self.repr(path).withCString(encodedAs: UTF16.self) { pwszPath in
var canonical: PWSTR!
_ = PathAllocCanonicalize(pwszPath,
ULONG(PATHCCH_ALLOW_LONG_PATHS.rawValue),
Expand All @@ -541,6 +544,14 @@ private struct WindowsPath: Path, Sendable {
})
}

init(validatingAbsolutePath path: String) throws {
let realpath = Self.repr(path)
if !Self.isAbsolutePath(realpath) {
throw PathValidationError.invalidAbsolutePath(path)
}
self.init(normalizingAbsolutePath: path)
}

init(normalizingRelativePath path: String) {
if path.isEmpty || path == "." {
self.init(string: ".")
Expand All @@ -553,28 +564,18 @@ private struct WindowsPath: Path, Sendable {
}
}

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

let realpath = String(cString: fsr)
if !Self.isAbsolutePath(realpath) {
throw PathValidationError.invalidAbsolutePath(path)
}
self.init(normalizingAbsolutePath: path)
}

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

let realpath: String = String(cString: fsr)
// Treat a relative path as an invalid relative path...
if Self.isAbsolutePath(realpath) ||
realpath.first == "~" || realpath.first == "\\" {
throw PathValidationError.invalidRelativePath(path)
if path.isEmpty || path == "." {
self.init(string: ".")
} else {
let realpath: String = Self.repr(path)
// Treat a relative path as an invalid relative path...
if Self.isAbsolutePath(realpath) ||
realpath.first == "~" || realpath.first == "\\" {
throw PathValidationError.invalidRelativePath(path)
}
self.init(normalizingRelativePath: path)
}
self.init(normalizingRelativePath: path)
}

func suffix(withDot: Bool) -> String? {
Expand Down