Skip to content

Commit 7988759

Browse files
committed
CoreFoundation: avoid CFString construction in CFURLSessionInterface
This causes a reference to `CFStringCreateWithBytes` and `kCFDefaultSystemAllocator` in `CFURLSessionInterface` which requires linking against CoreFoundation. Since the swift deployment uses CoreFoundation statically, this would require a second instance of CoreFoundation OR that private CoreFoundation interfaces are re-exposed through Foundation which is explicitly undesired. Perform the String construction in Swift and instead just have a trivial wrapper for `curl_easy_strerror`.
1 parent 55d52f2 commit 7988759

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

CoreFoundation/URL.subproj/CFURLSessionInterface.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ static CFURLSessionMultiCode MakeMultiCode(CURLMcode value) {
3232
return (CFURLSessionMultiCode) { value };
3333
}
3434

35-
CFStringRef CFURLSessionCreateErrorDescription(int value) {
36-
const char *description = curl_easy_strerror(value);
37-
return CFStringCreateWithBytes(kCFAllocatorSystemDefault,
38-
(const uint8_t *)description, strlen(description), kCFStringEncodingUTF8, NO);
35+
const char *CFURLSessionEasyCodeDescription(CFURLSessionEasyCode code) {
36+
return curl_easy_strerror(code.value);
3937
}
4038

4139
CFURLSessionEasyHandle _Nonnull CFURLSessionEasyHandleInit() {

CoreFoundation/URL.subproj/CFURLSessionInterface.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ typedef struct CFURLSessionEasyCode {
5656
int value;
5757
} CFURLSessionEasyCode;
5858

59-
CF_EXPORT CFStringRef _Nonnull CFURLSessionCreateErrorDescription(int value);
59+
CF_EXPORT const char * _Nullable CFURLSessionEasyCodeDescription(CFURLSessionEasyCode code);
6060

6161
CF_EXPORT int const CFURLSessionEasyErrorSize;
6262

Foundation/URLSession/libcurl/MultiHandle.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,14 @@ fileprivate extension URLSession._MultiHandle {
217217
// Find the NSURLError code
218218
var error: NSError?
219219
if let errorCode = easyHandle.urlErrorCode(for: easyCode) {
220-
let errorDescription = easyHandle.errorBuffer[0] != 0 ?
221-
String(cString: easyHandle.errorBuffer) :
222-
unsafeBitCast(CFURLSessionCreateErrorDescription(easyCode.value), to: NSString.self) as String
220+
var errorDescription: String = ""
221+
if easyHandle.errorBuffer[0] == 0 {
222+
let description = CFURLSessionEasyCodeDescription(easyCode)!
223+
errorDescription = NSString(bytes: UnsafeMutableRawPointer(mutating: description), length: strlen(description), encoding: String.Encoding.utf8.rawValue)! as String
224+
} else {
225+
errorDescription = String(cString: easyHandle.errorBuffer)
226+
}
227+
223228
error = NSError(domain: NSURLErrorDomain, code: errorCode, userInfo: [
224229
NSLocalizedDescriptionKey: errorDescription
225230
])

0 commit comments

Comments
 (0)