Skip to content

Commit

Permalink
Prevent crash when "AFImageDownloader identifier" is nil (which is ca…
Browse files Browse the repository at this point in the history
…use by passing an NSURLRequest which doesn't return an absoluteString). #3343. Added a test for this behavior.
  • Loading branch information
xianwenx authored and kcharwood committed Mar 14, 2016
1 parent 326ccb1 commit fa2c806
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Tests/Tests/AFImageDownloaderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ - (void)testThatImageDownloaderCanBeInitializedAndDeinitializedWithActiveDownloa
XCTAssertNil(self.downloader, @"Downloader should be nil");
}

- (void)testThatImageDownloaderReturnsNilWithInvalidURL
{
NSURL *pngURL = [NSURL URLWithString:@"https://httpbin.org/image/png"];
NSMutableURLRequest *mutableURLRequest = [NSMutableURLRequest requestWithURL:pngURL];
[mutableURLRequest setURL:nil];
/** NSURLRequest nor NSMutableURLRequest can be initialized with a nil URL,
* but NSMutableURLRequest can have its URL set to nil
**/
NSURLRequest *invalidRequest = [mutableURLRequest copy];
AFImageDownloadReceipt *downloadReceipt = [self.downloader downloadImageForURLRequest:invalidRequest
success:nil
failure:nil];

XCTAssertNil(downloadReceipt, @"downloadReceipt should be nil");
}

- (void)testThatImageDownloaderCanDownloadImage {
XCTestExpectation *expectation = [self expectationWithDescription:@"image download should succeed"];

Expand Down
3 changes: 3 additions & 0 deletions UIKit+AFNetworking/AFImageDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ - (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)
__block NSURLSessionDataTask *task = nil;
dispatch_sync(self.synchronizationQueue, ^{
NSString *URLIdentifier = request.URL.absoluteString;
if (URLIdentifier == nil) {
return;
}

// 1) Append the success and failure blocks to a pre-existing request if it already exists
AFImageDownloaderMergedTask *existingMergedTask = self.mergedTasks[URLIdentifier];
Expand Down

0 comments on commit fa2c806

Please sign in to comment.