Skip to content

Commit 33825e8

Browse files
committed
SwiftDriver: avoid using toSwiftString for target info
The C-String to Swift-String conversion would corrupt the path on Windows, where the path separator would get dropped. This would result in the inability to use the driver due to the failure to load the standard library as the SDK would not be consulted. Directly convert the raw buffer to a `Data` for the foreign representation. Note that this also makes the code clearer as we explicitly see the copying as opposed to the use of the deprecated `String(bytesNoCopy:length:encoding:freeWhenDone:)` function. Thanks to @artemcm for the help with this!
1 parent f4b0ef0 commit 33825e8

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Sources/SwiftDriver/SwiftScan/SwiftScan.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,15 @@ internal extension swiftscan_diagnostic_severity_t {
339339
invocationStringArray)
340340
}
341341

342-
let targetInfoString: String = try compilerExecutablePath.description.withCString { cstring in
343-
let targetInfoStringRef = api.swiftscan_compiler_target_info_query_v2(invocation, cstring)
344-
defer { api.swiftscan_string_dispose(targetInfoStringRef) }
345-
return try toSwiftString(targetInfoStringRef)
342+
return try compilerExecutablePath.description.withCString {
343+
let info = api.swiftscan_compiler_target_info_query_v2(invocation, $0)
344+
defer { api.swiftscan_string_dispose(info) }
345+
guard let data = info.data else {
346+
throw DependencyScanningError.invalidStringPtr
347+
}
348+
return Data(buffer: UnsafeBufferPointer(start: data.bindMemory(to: CChar.self, capacity: info.length),
349+
count: info.length))
346350
}
347-
348-
let targetInfoData = Data(targetInfoString.utf8)
349-
return targetInfoData
350351
}
351352
}
352353

0 commit comments

Comments
 (0)