Skip to content

CoreFoundation: avoid CFString construction in CFURLSessionINterface #2302

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
May 26, 2019
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
6 changes: 2 additions & 4 deletions CoreFoundation/URL.subproj/CFURLSessionInterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion CoreFoundation/URL.subproj/CFURLSessionInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
11 changes: 8 additions & 3 deletions Foundation/URLSession/libcurl/MultiHandle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
])
Expand Down