+[WebHTMLRepresentation supportedMIMETypes] leaks
authorap@apple.com <ap@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 16 Jun 2016 01:22:54 +0000 (01:22 +0000)
committerap@apple.com <ap@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 16 Jun 2016 01:22:54 +0000 (01:22 +0000)
https://bugs.webkit.org/show_bug.cgi?id=158683

Reviewed by Darin Adler.

The problem occurred when chaining newArrayByConcatenatingArrays calls.

Also refactored the code to avoid returning NSMutableArrays disguised as NSArrays,
and removed __unsafe_unretained modifiers that were added in http://trac.webkit.org/r149453
for no apparent reason.

* WebView/WebHTMLRepresentation.mm:
(newArrayWithStrings):
(+[WebHTMLRepresentation supportedMIMETypes]):
(+[WebHTMLRepresentation supportedMediaMIMETypes]):
(+[WebHTMLRepresentation supportedNonImageMIMETypes]):
(+[WebHTMLRepresentation supportedImageMIMETypes]):
(+[WebHTMLRepresentation unsupportedTextMIMETypes]):
(newArrayByConcatenatingArrays): Deleted.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@202118 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebKit/mac/ChangeLog
Source/WebKit/mac/WebView/WebHTMLRepresentation.mm

index 813a4c1..757d2a0 100644 (file)
@@ -1,3 +1,25 @@
+2016-06-15  Alexey Proskuryakov  <ap@apple.com>
+
+        +[WebHTMLRepresentation supportedMIMETypes] leaks
+        https://bugs.webkit.org/show_bug.cgi?id=158683
+
+        Reviewed by Darin Adler.
+
+        The problem occurred when chaining newArrayByConcatenatingArrays calls.
+
+        Also refactored the code to avoid returning NSMutableArrays disguised as NSArrays,
+        and removed __unsafe_unretained modifiers that were added in http://trac.webkit.org/r149453
+        for no apparent reason.
+
+        * WebView/WebHTMLRepresentation.mm:
+        (newArrayWithStrings):
+        (+[WebHTMLRepresentation supportedMIMETypes]):
+        (+[WebHTMLRepresentation supportedMediaMIMETypes]):
+        (+[WebHTMLRepresentation supportedNonImageMIMETypes]):
+        (+[WebHTMLRepresentation supportedImageMIMETypes]):
+        (+[WebHTMLRepresentation unsupportedTextMIMETypes]):
+        (newArrayByConcatenatingArrays): Deleted.
+
 2016-06-15  Dean Jackson  <dino@apple.com>
 
         RTL <select> forms are misplaced
index e8c76aa..a437890 100644 (file)
@@ -87,51 +87,42 @@ using JSC::Yarr::RegularExpression;
 
 @implementation WebHTMLRepresentation
 
-static NSMutableArray *newArrayWithStrings(const HashSet<String, ASCIICaseInsensitiveHash>&) NS_RETURNS_RETAINED;
-static NSMutableArray *newArrayWithStrings(const HashSet<String, ASCIICaseInsensitiveHash>& set)
+static RetainPtr<NSArray> newArrayWithStrings(const HashSet<String, ASCIICaseInsensitiveHash>& set)
 {
-    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:set.size()];
-    for (auto& string : set)
-        [array addObject:(NSString *)string];
-    return array;
-}
-
-static NSMutableArray *newArrayByConcatenatingArrays(NSArray *first, NSArray *second) NS_RETURNS_RETAINED;
-static NSMutableArray *newArrayByConcatenatingArrays(NSArray *first, NSArray *second)
-{
-    NSMutableArray *result = [first mutableCopy];
-    [result addObjectsFromArray:second];
-    return result;
+    Vector<NSString *> vector;
+    copyToVector(set, vector);
+    return adoptNS([[NSArray alloc] initWithObjects:vector.data() count:vector.size()]);
 }
 
 + (NSArray *)supportedMIMETypes
 {
-    static __unsafe_unretained NSArray *staticSupportedMIMETypes = newArrayByConcatenatingArrays([self supportedNonImageMIMETypes],
-        newArrayByConcatenatingArrays([self supportedImageMIMETypes], [self supportedMediaMIMETypes]));
+    static NSArray *staticSupportedMIMETypes = [[[[self supportedNonImageMIMETypes] arrayByAddingObjectsFromArray:
+        [self supportedImageMIMETypes]] arrayByAddingObjectsFromArray:
+        [self supportedMediaMIMETypes]] retain];
     return staticSupportedMIMETypes;
 }
 
 + (NSArray *)supportedMediaMIMETypes
 {
-    static __unsafe_unretained NSArray *staticSupportedMediaMIMETypes = newArrayWithStrings(MIMETypeRegistry::getSupportedMediaMIMETypes());
+    static NSArray *staticSupportedMediaMIMETypes = newArrayWithStrings(MIMETypeRegistry::getSupportedMediaMIMETypes()).leakRef();
     return staticSupportedMediaMIMETypes;
 }
 
 + (NSArray *)supportedNonImageMIMETypes
 {
-    static __unsafe_unretained NSArray *staticSupportedNonImageMIMETypes = newArrayWithStrings(MIMETypeRegistry::getSupportedNonImageMIMETypes());
+    static NSArray *staticSupportedNonImageMIMETypes = newArrayWithStrings(MIMETypeRegistry::getSupportedNonImageMIMETypes()).leakRef();
     return staticSupportedNonImageMIMETypes;
 }
 
 + (NSArray *)supportedImageMIMETypes
 {
-    static __unsafe_unretained NSArray *staticSupportedImageMIMETypes = newArrayWithStrings(MIMETypeRegistry::getSupportedImageMIMETypes());
+    static NSArray *staticSupportedImageMIMETypes = newArrayWithStrings(MIMETypeRegistry::getSupportedImageMIMETypes()).leakRef();
     return staticSupportedImageMIMETypes;
 }
 
 + (NSArray *)unsupportedTextMIMETypes
 {
-    static __unsafe_unretained NSArray *staticUnsupportedTextMIMETypes = newArrayWithStrings(MIMETypeRegistry::getUnsupportedTextMIMETypes());
+    static NSArray *staticUnsupportedTextMIMETypes = newArrayWithStrings(MIMETypeRegistry::getUnsupportedTextMIMETypes()).leakRef();
     return staticUnsupportedTextMIMETypes;
 }