From 79887591f57ea7d1e4e2c49b3c2ae18476f8e9c3 Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Fri, 24 May 2019 22:31:35 -0700 Subject: [PATCH] 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`. --- CoreFoundation/URL.subproj/CFURLSessionInterface.c | 6 ++---- CoreFoundation/URL.subproj/CFURLSessionInterface.h | 2 +- Foundation/URLSession/libcurl/MultiHandle.swift | 11 ++++++++--- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CoreFoundation/URL.subproj/CFURLSessionInterface.c b/CoreFoundation/URL.subproj/CFURLSessionInterface.c index 4ef31bac4e..31356589b5 100644 --- a/CoreFoundation/URL.subproj/CFURLSessionInterface.c +++ b/CoreFoundation/URL.subproj/CFURLSessionInterface.c @@ -32,10 +32,8 @@ static CFURLSessionMultiCode MakeMultiCode(CURLMcode value) { return (CFURLSessionMultiCode) { value }; } -CFStringRef CFURLSessionCreateErrorDescription(int value) { - const char *description = curl_easy_strerror(value); - return CFStringCreateWithBytes(kCFAllocatorSystemDefault, - (const uint8_t *)description, strlen(description), kCFStringEncodingUTF8, NO); +const char *CFURLSessionEasyCodeDescription(CFURLSessionEasyCode code) { + return curl_easy_strerror(code.value); } CFURLSessionEasyHandle _Nonnull CFURLSessionEasyHandleInit() { diff --git a/CoreFoundation/URL.subproj/CFURLSessionInterface.h b/CoreFoundation/URL.subproj/CFURLSessionInterface.h index 90bd156bba..5521e9ce64 100644 --- a/CoreFoundation/URL.subproj/CFURLSessionInterface.h +++ b/CoreFoundation/URL.subproj/CFURLSessionInterface.h @@ -56,7 +56,7 @@ typedef struct CFURLSessionEasyCode { int value; } CFURLSessionEasyCode; -CF_EXPORT CFStringRef _Nonnull CFURLSessionCreateErrorDescription(int value); +CF_EXPORT const char * _Nullable CFURLSessionEasyCodeDescription(CFURLSessionEasyCode code); CF_EXPORT int const CFURLSessionEasyErrorSize; diff --git a/Foundation/URLSession/libcurl/MultiHandle.swift b/Foundation/URLSession/libcurl/MultiHandle.swift index d5819539c3..13d246e8f1 100644 --- a/Foundation/URLSession/libcurl/MultiHandle.swift +++ b/Foundation/URLSession/libcurl/MultiHandle.swift @@ -217,9 +217,14 @@ fileprivate extension URLSession._MultiHandle { // Find the NSURLError code var error: NSError? if let errorCode = easyHandle.urlErrorCode(for: easyCode) { - let errorDescription = easyHandle.errorBuffer[0] != 0 ? - String(cString: easyHandle.errorBuffer) : - unsafeBitCast(CFURLSessionCreateErrorDescription(easyCode.value), to: NSString.self) as String + var errorDescription: String = "" + if easyHandle.errorBuffer[0] == 0 { + let description = CFURLSessionEasyCodeDescription(easyCode)! + errorDescription = NSString(bytes: UnsafeMutableRawPointer(mutating: description), length: strlen(description), encoding: String.Encoding.utf8.rawValue)! as String + } else { + errorDescription = String(cString: easyHandle.errorBuffer) + } + error = NSError(domain: NSURLErrorDomain, code: errorCode, userInfo: [ NSLocalizedDescriptionKey: errorDescription ])