Skip to content

Commit bd00544

Browse files
committed
Fix absolute path validation on Windows
1 parent 5b0e9a8 commit bd00544

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

Sources/TSCBasic/Path.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,12 @@ private struct UNIXPath: Path {
397397
return name != "" && name != "." && name != ".." && !name.contains("/")
398398
}
399399

400+
#if os(Windows)
401+
static func isAbsolutePath(_ path: String) -> Bool {
402+
return !path.withCString(encodedAs: UTF16.self, PathIsRelativeW)
403+
}
404+
#endif
405+
400406
var dirname: String {
401407
#if os(Windows)
402408
let dir = string.deletingLastPathComponent
@@ -421,7 +427,11 @@ private struct UNIXPath: Path {
421427
}
422428

423429
var isAbsolute: Bool {
424-
string.hasPrefix("/")
430+
#if os(Windows)
431+
return UNIXPath.isAbsolutePath(string)
432+
#else
433+
return string.hasPrefix("/")
434+
#endif
425435
}
426436

427437
var basename: String {
@@ -608,7 +618,7 @@ private struct UNIXPath: Path {
608618
defer { fsr.deallocate() }
609619

610620
let realpath = String(cString: fsr)
611-
if realpath.withCString(encodedAs: UTF16.self, PathIsRelativeW) {
621+
if !UNIXPath.isAbsolutePath(realpath) {
612622
throw PathValidationError.invalidAbsolutePath(path)
613623
}
614624
self.init(normalizingAbsolutePath: path)
@@ -630,7 +640,7 @@ private struct UNIXPath: Path {
630640
defer { fsr.deallocate() }
631641

632642
let realpath: String = String(cString: fsr)
633-
if !realpath.withCString(encodedAs: UTF16.self, PathIsRelativeW) {
643+
if UNIXPath.isAbsolutePath(realpath) {
634644
throw PathValidationError.invalidRelativePath(path)
635645
}
636646
self.init(normalizingRelativePath: path)

Sources/TSCBasic/misc.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ public func getEnvSearchPaths(
4848
currentWorkingDirectory: AbsolutePath?
4949
) -> [AbsolutePath] {
5050
// Compute search paths from PATH variable.
51-
return (pathString ?? "").split(separator: ":").map(String.init).compactMap({ pathString in
52-
// If this is an absolute path, we're done.
53-
if pathString.first == "/" {
54-
return AbsolutePath(pathString)
55-
}
56-
// Otherwise convert it into absolute path relative to the working directory.
57-
guard let cwd = currentWorkingDirectory else {
58-
return nil
51+
#if os(Windows)
52+
let pathSeparator: Character = ";"
53+
#else
54+
let pathSeparator: Character = ":"
55+
#endif
56+
return (pathString ?? "").split(separator: pathSeparator).map(String.init).compactMap({ pathString in
57+
if let cwd = currentWorkingDirectory {
58+
return AbsolutePath(pathString, relativeTo: cwd)
5959
}
60-
return AbsolutePath(pathString, relativeTo: cwd)
60+
return try? AbsolutePath(validating: pathString)
6161
})
6262
}
6363

0 commit comments

Comments
 (0)