Skip to content

TSCBasic: protect against an invalid API usage #325

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

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 17 additions & 1 deletion Sources/TSCBasic/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ private typealias PathImpl = UNIXPath
private typealias PathImpl = UNIXPath
#endif

#if canImport(Darwin)
import Darwin.C
#endif

import class Foundation.NSURL
import protocol Foundation.CustomNSError
import var Foundation.NSLocalizedDescriptionKey

Expand Down Expand Up @@ -254,7 +259,18 @@ public struct RelativePath: Hashable {
/// normalization or canonicalization. This will construct a path without
/// an anchor and thus may be invalid.
fileprivate init(unsafeUncheckedPath string: String) {
self.init(PathImpl(string: string))
if string.isEmpty {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the case need to be a guard and then return a ~const empty representation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the case need to be a guard and then return a ~const empty representation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can change that of course, but that doesn't help the macOS case. It would be purely a syntactic change and should be equivalent to this.

self.init(PathImpl(string: string))
} else {
#if _runtime(_ObjC)
self.init(PathImpl(string: String(cString: NSURL(fileURLWithPath: string).fileSystemRepresentation)))
#else
let normalized: UnsafePointer<Int8> = string.fileSystemRepresentation
defer { normalized.deallocate() }

self.init(PathImpl(string: String(cString: normalized)))
#endif
}
}

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