[Cocoa] fix CF leaks found by code inspection
authordarin@apple.com <darin@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 24 Apr 2014 04:46:28 +0000 (04:46 +0000)
committerdarin@apple.com <darin@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 24 Apr 2014 04:46:28 +0000 (04:46 +0000)
https://bugs.webkit.org/show_bug.cgi?id=132106

Reviewed by Andreas Kling.

* page/CaptionUserPreferencesMediaAF.cpp:
(WebCore::trackDisplayName): Added a missing adoptCF.

* platform/Language.cpp:
(WebCore::displayNameForLanguageLocale): Added a missing adoptCF.

* platform/graphics/FontPlatformData.cpp:
(WebCore::FontPlatformData::openTypeTable): Added a missing adoptCF.

* platform/graphics/avfoundation/cf/InbandTextTrackPrivateAVCF.cpp:
(WebCore::InbandTextTrackPrivateAVCF::label): Added two missing adoptCF.

* platform/graphics/avfoundation/cf/MediaPlayerPrivateAVFoundationCF.cpp:
(WebCore::AVFWrapper::createImageForTimeInRect): Added two missing adoptCF.

* platform/graphics/cg/PDFDocumentImage.cpp:
(WebCore::PDFDocumentImage::createPDFDocument): Added missing adoptCF.

* platform/graphics/cocoa/FontPlatformDataCocoa.mm:
(WebCore::cascadeToLastResortFontDescriptor): Added two missing adoptCF.

* platform/graphics/mac/FontMac.mm:
(WebCore::Font::primaryFontDataIsSystemFont): Added missing adoptCF.

* platform/graphics/mac/SimpleFontDataMac.mm:
(WebCore::hasCustomTracking): Added missing adoptCF.

* platform/image-decoders/ImageDecoder.h:
(WebCore::ImageDecoder::qcmsOutputDeviceProfile): Added CFRelease.

* plugins/mac/PluginPackageMac.cpp:
(WebCore::readPListFile): Added two missing adoptCF.

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

12 files changed:
Source/WebCore/ChangeLog
Source/WebCore/page/CaptionUserPreferencesMediaAF.cpp
Source/WebCore/platform/Language.cpp
Source/WebCore/platform/graphics/FontPlatformData.cpp
Source/WebCore/platform/graphics/avfoundation/cf/InbandTextTrackPrivateAVCF.cpp
Source/WebCore/platform/graphics/avfoundation/cf/MediaPlayerPrivateAVFoundationCF.cpp
Source/WebCore/platform/graphics/cg/PDFDocumentImage.cpp
Source/WebCore/platform/graphics/cocoa/FontPlatformDataCocoa.mm
Source/WebCore/platform/graphics/mac/FontMac.mm
Source/WebCore/platform/graphics/mac/SimpleFontDataMac.mm
Source/WebCore/platform/image-decoders/ImageDecoder.h
Source/WebCore/plugins/mac/PluginPackageMac.cpp

index 482a4a6..7cbb003 100644 (file)
@@ -1,3 +1,43 @@
+2014-04-23  Darin Adler  <darin@apple.com>
+
+        [Cocoa] fix CF leaks found by code inspection
+        https://bugs.webkit.org/show_bug.cgi?id=132106
+
+        Reviewed by Andreas Kling.
+
+        * page/CaptionUserPreferencesMediaAF.cpp:
+        (WebCore::trackDisplayName): Added a missing adoptCF.
+
+        * platform/Language.cpp:
+        (WebCore::displayNameForLanguageLocale): Added a missing adoptCF.
+
+        * platform/graphics/FontPlatformData.cpp:
+        (WebCore::FontPlatformData::openTypeTable): Added a missing adoptCF.
+
+        * platform/graphics/avfoundation/cf/InbandTextTrackPrivateAVCF.cpp:
+        (WebCore::InbandTextTrackPrivateAVCF::label): Added two missing adoptCF.
+
+        * platform/graphics/avfoundation/cf/MediaPlayerPrivateAVFoundationCF.cpp:
+        (WebCore::AVFWrapper::createImageForTimeInRect): Added two missing adoptCF.
+
+        * platform/graphics/cg/PDFDocumentImage.cpp:
+        (WebCore::PDFDocumentImage::createPDFDocument): Added missing adoptCF.
+
+        * platform/graphics/cocoa/FontPlatformDataCocoa.mm:
+        (WebCore::cascadeToLastResortFontDescriptor): Added two missing adoptCF.
+
+        * platform/graphics/mac/FontMac.mm:
+        (WebCore::Font::primaryFontDataIsSystemFont): Added missing adoptCF.
+
+        * platform/graphics/mac/SimpleFontDataMac.mm:
+        (WebCore::hasCustomTracking): Added missing adoptCF.
+
+        * platform/image-decoders/ImageDecoder.h:
+        (WebCore::ImageDecoder::qcmsOutputDeviceProfile): Added CFRelease.
+
+        * plugins/mac/PluginPackageMac.cpp:
+        (WebCore::readPListFile): Added two missing adoptCF.
+
 2014-04-23  Ryosuke Niwa  <rniwa@webkit.org>
 
         REGRESSION (r157328): popover to check into flight ba.com dismisses instantly when focusing form
index 16f1147..573332d 100644 (file)
@@ -608,7 +608,7 @@ static String trackDisplayName(TextTrack* track)
             }
         }
     } else {
-        String languageAndLocale = CFLocaleCopyDisplayNameForPropertyValue(currentLocale.get(), kCFLocaleIdentifier, trackLanguageIdentifier.createCFString().get());
+        String languageAndLocale = adoptCF(CFLocaleCopyDisplayNameForPropertyValue(currentLocale.get(), kCFLocaleIdentifier, trackLanguageIdentifier.createCFString().get())).get();
         if (!languageAndLocale.isEmpty())
             displayName.append(languageAndLocale);
         else if (!language.isEmpty())
index f5683d0..c2892e4 100644 (file)
@@ -148,10 +148,8 @@ size_t indexOfBestMatchingLanguageInList(const String& language, const Vector<St
 String displayNameForLanguageLocale(const String& localeName)
 {
 #if USE(CF) && !PLATFORM(WIN)
-    if (!localeName.isNull() && !localeName.isEmpty()) {
-        RetainPtr<CFLocaleRef> currentLocale = adoptCF(CFLocaleCopyCurrent());
-        return CFLocaleCopyDisplayNameForPropertyValue(currentLocale.get(), kCFLocaleIdentifier, localeName.createCFString().get());
-    }
+    if (!localeName.isEmpty())
+        return adoptCF(CFLocaleCopyDisplayNameForPropertyValue(adoptCF(CFLocaleCopyCurrent()).get(), kCFLocaleIdentifier, localeName.createCFString().get())).get();
 #endif
     return localeName;
 }
index 19d6761..a80fd92 100644 (file)
@@ -177,8 +177,8 @@ const FontPlatformData& FontPlatformData::operator=(const FontPlatformData& othe
 #if OS(DARWIN) && USE(CG)
 PassRefPtr<SharedBuffer> FontPlatformData::openTypeTable(uint32_t table) const
 {
-    if (CFDataRef data = CGFontCopyTableForTag(cgFont(), table))
-        return SharedBuffer::wrapCFData(data);
+    if (RetainPtr<CFDataRef> data = adoptCF(CGFontCopyTableForTag(cgFont(), table)))
+        return SharedBuffer::wrapCFData(data.get());
     
     return nullptr;
 }
index 1eecba5..2abb72d 100644 (file)
@@ -148,8 +148,8 @@ AtomicString InbandTextTrackPrivateAVCF::label() const
         return emptyAtom;
 
     // If possible, return a title in one of the user's preferred languages.
-    RetainPtr<CFArrayRef> preferredLanguages = CFLocaleCopyPreferredLanguages();
-    RetainPtr<CFArrayRef> titlesForPreferredLanguages = AVCFMediaSelectionCopyOptionsFromArrayFilteredAndSortedAccordingToPreferredLanguages(titles.get(), preferredLanguages.get());
+    RetainPtr<CFArrayRef> preferredLanguages = adoptCF(CFLocaleCopyPreferredLanguages());
+    RetainPtr<CFArrayRef> titlesForPreferredLanguages = adoptCF(AVCFMediaSelectionCopyOptionsFromArrayFilteredAndSortedAccordingToPreferredLanguages(titles.get(), preferredLanguages.get()));
     CFIndex preferredTitlesCount = CFArrayGetCount(titlesForPreferredLanguages.get());
     if (preferredTitlesCount) {
         AVCFMetadataItemRef titleMetadata = static_cast<AVCFMetadataItemRef>(CFArrayGetValueAtIndex(titlesForPreferredLanguages.get(), 0));
index 292c6c5..7da675d 100644 (file)
@@ -1757,8 +1757,9 @@ RetainPtr<CGImageRef> AVFWrapper::createImageForTimeInRect(float time, const Int
 #endif
 
     AVCFAssetImageGeneratorSetMaximumSize(m_imageGenerator.get(), CGSize(rect.size()));
-    CGImageRef rawimage = AVCFAssetImageGeneratorCopyCGImageAtTime(m_imageGenerator.get(), CMTimeMakeWithSeconds(time, 600), 0, 0);
-    CGImageRef image = CGImageCreateCopyWithColorSpace(rawimage,  CGColorSpaceCreateDeviceRGB());
+    RetainPtr<CGImageRef> rawimage = adoptCF(AVCFAssetImageGeneratorCopyCGImageAtTime(m_imageGenerator.get(), CMTimeMakeWithSeconds(time, 600), 0, 0));
+    RetainPtr<CGImageRef> image = adoptCF(CGImageCreateCopyWithColorSpace(rawimage.get(), adoptCF(CGColorSpaceCreateDeviceRGB()).get()));
+
 #if !LOG_DISABLED
     double duration = monotonicallyIncreasingTime() - start;
     LOG(Media, "AVFWrapper::createImageForTimeInRect(%p) - creating image took %.4f", this, narrowPrecisionToFloat(duration));
index 9fe9101..4022fad 100644 (file)
@@ -209,10 +209,11 @@ void PDFDocumentImage::destroyDecodedData(bool)
 }
 
 #if !USE(PDFKIT_FOR_PDFDOCUMENTIMAGE)
+
 void PDFDocumentImage::createPDFDocument()
 {
     RetainPtr<CGDataProviderRef> dataProvider = adoptCF(CGDataProviderCreateWithCFData(data()->createCFData().get()));
-    m_document = CGPDFDocumentCreateWithProvider(dataProvider.get());
+    m_document = adoptCF(CGPDFDocumentCreateWithProvider(dataProvider.get()));
 }
 
 void PDFDocumentImage::computeBoundsForCurrentPage()
@@ -256,6 +257,7 @@ void PDFDocumentImage::drawPDFPage(GraphicsContext* context)
     // CGPDF pages are indexed from 1.
     CGContextDrawPDFPage(context->platformContext(), CGPDFDocumentGetPage(m_document.get(), 1));
 }
+
 #endif // !USE(PDFKIT_FOR_PDFDOCUMENTIMAGE)
 
 }
index 2e7fdd9..3ff1823 100644 (file)
@@ -272,9 +272,13 @@ static CTFontDescriptorRef cascadeToLastResortFontDescriptor()
     if (descriptor)
         return descriptor;
 
+    RetainPtr<CTFontDescriptorRef> lastResort = adoptCF(CTFontDescriptorCreateWithNameAndSize(CFSTR("LastResort"), 0));
+
+    const void* descriptors[] = { lastResort.get() };
+    RetainPtr<CFArrayRef> array = adoptCF(CFArrayCreate(kCFAllocatorDefault, descriptors, WTF_ARRAY_LENGTH(descriptors), &kCFTypeArrayCallBacks));
+
     const void* keys[] = { kCTFontCascadeListAttribute };
-    const void* descriptors[] = { CTFontDescriptorCreateWithNameAndSize(CFSTR("LastResort"), 0) };
-    const void* values[] = { CFArrayCreate(kCFAllocatorDefault, descriptors, WTF_ARRAY_LENGTH(descriptors), &kCFTypeArrayCallBacks) };
+    const void* values[] = { array.get() };
     RetainPtr<CFDictionaryRef> attributes = adoptCF(CFDictionaryCreate(kCFAllocatorDefault, keys, values, WTF_ARRAY_LENGTH(keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
 
     descriptor = CTFontDescriptorCreateWithAttributes(attributes.get());
index e1eee2d..75de45e 100644 (file)
@@ -543,8 +543,7 @@ DashArray Font::dashesForIntersectionsWithRect(const TextRun& run, const FloatPo
 bool Font::primaryFontDataIsSystemFont() const
 {
 #if PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED > 1090
-    RetainPtr<CTFontDescriptorRef> descriptor = CTFontCopyFontDescriptor(primaryFont()->platformData().ctFont());
-    return CTFontDescriptorIsSystemUIFont(descriptor.get());
+    return CTFontDescriptorIsSystemUIFont(adoptCF(CTFontCopyFontDescriptor(primaryFont()->platformData().ctFont())).get());
 #else
     // System fonts are hidden by having a name that begins with a period, so simply search
     // for that here rather than try to keep the list up to date.
index a8dd50e..72357fa 100644 (file)
@@ -372,8 +372,7 @@ static bool hasCustomTracking(CTFontRef font)
     UNUSED_PARAM(font);
     return false;
 #else
-    RetainPtr<CTFontDescriptorRef> descriptor = CTFontCopyFontDescriptor(font);
-    return CTFontDescriptorIsSystemUIFont(descriptor.get());
+    return CTFontDescriptorIsSystemUIFont(adoptCF(CTFontCopyFontDescriptor(font)).get());
 #endif
 }
 
index f4a1794..28f0a95 100644 (file)
@@ -320,6 +320,7 @@ namespace WebCore {
                     size_t length = CFDataGetLength(iccProfile);
                     const unsigned char* systemProfile = CFDataGetBytePtr(iccProfile);
                     outputDeviceProfile = qcms_profile_from_memory(systemProfile, length);
+                    CFRelease(iccProfile);
                 }
 #else
                 // FIXME: add support for multiple monitors.
index fee27ed..703a41d 100644 (file)
 #include "config.h"
 #include "PluginPackage.h"
 
-#include <wtf/RetainPtr.h>
 #include "MIMETypeRegistry.h"
-#include "npruntime_impl.h"
 #include "PluginDatabase.h"
 #include "PluginDebug.h"
 #include "WebCoreNSStringExtras.h"
-#include <wtf/text/CString.h>
-
+#include "npruntime_impl.h"
 #include <CoreFoundation/CoreFoundation.h>
+#include <wtf/RetainPtr.h>
+#include <wtf/text/CString.h>
 
 #define PluginNameOrDescriptionStringNumber     126
 #define MIMEDescriptionStringNumber             127
@@ -67,7 +66,7 @@ void PluginPackage::determineQuirks(const String& mimeType)
 
 typedef void (*BP_CreatePluginMIMETypesPreferencesFuncPtr)(void);
 
-static WTF::RetainPtr<CFDictionaryRef> readPListFile(CFStringRef fileName, bool createFile, CFBundleRef bundle)
+static RetainPtr<CFDictionaryRef> readPListFile(CFStringRef fileName, bool createFile, CFBundleRef bundle)
 {
     if (createFile) {
         BP_CreatePluginMIMETypesPreferencesFuncPtr funcPtr =
@@ -76,17 +75,15 @@ static WTF::RetainPtr<CFDictionaryRef> readPListFile(CFStringRef fileName, bool
             funcPtr();
     }
 
-    WTF::RetainPtr<CFDictionaryRef> map;
-    WTF::RetainPtr<CFURLRef> url =
-        CFURLCreateWithFileSystemPath(kCFAllocatorDefault, fileName, kCFURLPOSIXPathStyle, false);
+    RetainPtr<CFDictionaryRef> map;
+    RetainPtr<CFURLRef> url = adoptCF(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, fileName, kCFURLPOSIXPathStyle, false));
 
     CFDataRef resource = 0;
     SInt32 code;
     if (!CFURLCreateDataAndPropertiesFromResource(kCFAllocatorDefault, url.get(), &resource, 0, 0, &code))
         return map;
 
-    WTF::RetainPtr<CFPropertyListRef> propertyList =
-            CFPropertyListCreateFromXMLData(kCFAllocatorDefault, resource, kCFPropertyListImmutable, 0);
+    RetainPtr<CFPropertyListRef> propertyList = adoptCF(CFPropertyListCreateFromXMLData(kCFAllocatorDefault, resource, kCFPropertyListImmutable, 0));
 
     CFRelease(resource);
 
@@ -119,7 +116,7 @@ static Vector<String> stringListFromResourceId(SInt16 id)
 
     for (SInt16 i = 0; i < count; ++i) {
         unsigned char length = *p;
-        WTF::RetainPtr<CFStringRef> str = adoptCF(CFStringCreateWithPascalString(0, p, encoding));
+        RetainPtr<CFStringRef> str = adoptCF(CFStringCreateWithPascalString(0, p, encoding));
         list.append(str.get());
         p += 1 + length;
     }
@@ -132,19 +129,19 @@ bool PluginPackage::fetchInfo()
     if (!load())
         return false;
 
-    WTF::RetainPtr<CFDictionaryRef> mimeDict;
+    RetainPtr<CFDictionaryRef> mimeDict;
 
-    WTF::RetainPtr<CFTypeRef> mimeTypesFileName = CFBundleGetValueForInfoDictionaryKey(m_module, CFSTR("WebPluginMIMETypesFilename"));
+    RetainPtr<CFTypeRef> mimeTypesFileName = CFBundleGetValueForInfoDictionaryKey(m_module, CFSTR("WebPluginMIMETypesFilename"));
     if (mimeTypesFileName && CFGetTypeID(mimeTypesFileName.get()) == CFStringGetTypeID()) {
 
-        WTF::RetainPtr<CFStringRef> fileName = (CFStringRef)mimeTypesFileName.get();
-        WTF::RetainPtr<CFStringRef> homeDir = adoptCF(homeDirectoryPath().createCFString());
-        WTF::RetainPtr<CFStringRef> path = adoptCF(CFStringCreateWithFormat(0, 0, CFSTR("%@/Library/Preferences/%@"), homeDir.get(), fileName.get()));
+        RetainPtr<CFStringRef> fileName = (CFStringRef)mimeTypesFileName.get();
+        RetainPtr<CFStringRef> homeDir = homeDirectoryPath().createCFString();
+        RetainPtr<CFStringRef> path = adoptCF(CFStringCreateWithFormat(0, 0, CFSTR("%@/Library/Preferences/%@"), homeDir.get(), fileName.get()));
 
-        WTF::RetainPtr<CFDictionaryRef> plist = readPListFile(path.get(), /*createFile*/ false, m_module);
+        RetainPtr<CFDictionaryRef> plist = readPListFile(path.get(), /*createFile*/ false, m_module);
         if (plist) {
             // If the plist isn't localized, have the plug-in recreate it in the preferred language.
-            WTF::RetainPtr<CFStringRef> localizationName =
+            RetainPtr<CFStringRef> localizationName =
                 (CFStringRef)CFDictionaryGetValue(plist.get(), CFSTR("WebPluginLocalizationName"));
             CFLocaleRef locale = CFLocaleCopyCurrent();
             if (localizationName != CFLocaleGetIdentifier(locale))
@@ -172,9 +169,9 @@ bool PluginPackage::fetchInfo()
             String mimeType = (CFStringRef)keys[i];
             mimeType = mimeType.lower();
 
-            WTF::RetainPtr<CFDictionaryRef> extensionsDict = (CFDictionaryRef)values[i];
+            RetainPtr<CFDictionaryRef> extensionsDict = (CFDictionaryRef)values[i];
 
-            WTF::RetainPtr<CFNumberRef> enabled = (CFNumberRef)CFDictionaryGetValue(extensionsDict.get(), CFSTR("WebPluginTypeEnabled"));
+            RetainPtr<CFNumberRef> enabled = (CFNumberRef)CFDictionaryGetValue(extensionsDict.get(), CFSTR("WebPluginTypeEnabled"));
             if (enabled) {
                 int enabledValue = 0;
                 if (CFNumberGetValue(enabled.get(), kCFNumberIntType, &enabledValue) && enabledValue == 0)
@@ -182,7 +179,7 @@ bool PluginPackage::fetchInfo()
             }
 
             Vector<String> mimeExtensions;
-            WTF::RetainPtr<CFArrayRef> extensions = (CFArrayRef)CFDictionaryGetValue(extensionsDict.get(), CFSTR("WebPluginExtensions"));
+            RetainPtr<CFArrayRef> extensions = (CFArrayRef)CFDictionaryGetValue(extensionsDict.get(), CFSTR("WebPluginExtensions"));
             if (extensions) {
                 CFIndex extensionCount = CFArrayGetCount(extensions.get());
                 for (CFIndex i = 0; i < extensionCount; ++i) {
@@ -255,8 +252,8 @@ bool PluginPackage::load()
         return true;
     }
 
-    WTF::RetainPtr<CFStringRef> path = adoptCF(m_path.createCFString());
-    WTF::RetainPtr<CFURLRef> url = adoptCF(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path.get(),
+    RetainPtr<CFStringRef> path = m_path.createCFString();
+    RetainPtr<CFURLRef> url = adoptCF(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path.get(),
                                                                         kCFURLPOSIXPathStyle, false));
     m_module = CFBundleCreate(NULL, url.get());
     if (!m_module || !CFBundleLoadExecutable(m_module)) {