Skip to content

Use system-appropriate method when quoting path arguments on command-lines #1343

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 1 commit into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions Sources/SwiftDriver/Execution/ArgsResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public final class ArgsResolver {
private func unsafeResolve(path: VirtualPath, quotePaths: Bool) throws -> String {
// If there was a path mapping, use it.
if let actualPath = pathMapping[path] {
return quotePaths ? "'\(actualPath)'" : actualPath
return quotePaths ? quoteArgument(actualPath) : actualPath
}

// Return the path from the temporary directory if this is a temporary file.
Expand Down Expand Up @@ -146,7 +146,7 @@ public final class ArgsResolver {
// Otherwise, return the path.
let result = path.name
pathMapping[path] = result
return quotePaths ? "'\(result)'" : result
return quotePaths ? quoteArgument(result) : result
}

private func createFileList(path: VirtualPath, contents: [VirtualPath]) throws {
Expand Down
78 changes: 41 additions & 37 deletions Sources/SwiftDriver/Utilities/System.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,45 @@ import Darwin
import Glibc
#endif

func argumentNeedsQuoting(_ argument: String) -> Bool {
if argument.isEmpty { return false }
let chars: Set<Character> = Set("\t \"&'()*<>\\`^|\n")
return argument.firstIndex(where: { chars.contains($0) }) != argument.endIndex
}

func quoteArgument(_ argument: String) -> String {
#if os(Windows)
var unquoted: Substring = argument[...]
var quoted: String = "\""
while !unquoted.isEmpty {
guard let firstNonBS = unquoted.firstIndex(where: { $0 != "\\" }) else {
// The rest of the string is backslashes. Escape all of them and exit.
(0 ..< (2 * unquoted.count)).forEach { _ in quoted += "\\" }
break
}

let bsCount = unquoted.distance(from: unquoted.startIndex, to: firstNonBS)
if unquoted[firstNonBS] == "\"" {
// This is an embedded quote. Escape all preceding backslashes, then
// add one additional backslash to escape the quote.
(0 ..< (2 * bsCount + 1)).forEach { _ in quoted += "\\" }
quoted += "\""
} else {
// This is just a normal character. Don't escape any of the preceding
// backslashes, just append them as they are and then append the
// character.
(0 ..< bsCount).forEach { _ in quoted += "\\" }
quoted += "\(unquoted[firstNonBS])"
}

unquoted = unquoted.dropFirst(bsCount + 1)
}
return quoted + "\""
#else
return "'" + argument + "'"
#endif
}

#if canImport(Darwin) || os(Linux) || os(Android) || os(OpenBSD)
// Adapted from llvm::sys::commandLineFitsWithinSystemLimits.
func commandLineFitsWithinSystemLimits(path: String, args: [String]) -> Bool {
Expand Down Expand Up @@ -49,45 +88,10 @@ func commandLineFitsWithinSystemLimits(path: String, args: [String]) -> Bool {
#elseif os(Windows)
func commandLineFitsWithinSystemLimits(path: String, args: [String]) -> Bool {
func flattenWindowsCommandLine(_ arguments: [String]) -> String {
func argNeedsQuoting(_ argument: String) -> Bool {
if argument.isEmpty { return false }
let chars: Set<Character> = Set("\t \"&'()*<>\\`^|\n")
return argument.firstIndex(where: { chars.contains($0) }) != argument.endIndex
}

func quote(_ argument: String) -> String {
var unquoted: Substring = argument[...]
var quoted: String = "\""
while !unquoted.isEmpty {
guard let firstNonBS = unquoted.firstIndex(where: { $0 != "\\" }) else {
// The rest of the string is backslashes. Escape all of them and exit.
(0 ..< (2 * unquoted.count)).forEach { _ in quoted += "\\" }
break
}

let bsCount = unquoted.distance(from: unquoted.startIndex, to: firstNonBS)
if unquoted[firstNonBS] == "\"" {
// This is an embedded quote. Escape all preceding backslashes, then
// add one additional backslash to escape the quote.
(0 ..< (2 * bsCount + 1)).forEach { _ in quoted += "\\" }
quoted += "\""
} else {
// This is just a normal character. Don't escape any of the preceding
// backslashes, just append them as they are and then append the
// character.
(0 ..< bsCount).forEach { _ in quoted += "\\" }
quoted += "\(unquoted[firstNonBS])"
}

unquoted = unquoted.dropFirst(bsCount + 1)
}
return quoted + "\""
}

var quoted: String = ""
for arg in arguments {
if argNeedsQuoting(arg) {
quoted += quote(arg)
if argumentNeedsQuoting(arg) {
quoted += quoteArgument(arg)
} else {
quoted += arg
}
Expand Down