Browse Source

Prevent crash when "AFImageDownloader identifier" is nil (which is cause by passing an NSURLRequest which doesn't return an absoluteString). #3343. Added a test for this behavior.

Christian Wen 9 years ago
parent
commit
fa2c806a39
2 changed files with 19 additions and 0 deletions
  1. 16 0
      Tests/Tests/AFImageDownloaderTests.m
  2. 3 0
      UIKit+AFNetworking/AFImageDownloader.m

+ 16 - 0
Tests/Tests/AFImageDownloaderTests.m

@@ -64,6 +64,22 @@
     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"];
 

+ 3 - 0
UIKit+AFNetworking/AFImageDownloader.m

@@ -189,6 +189,9 @@
     __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];