All calls of ImageBuffer::create should null check the return value
authorbfulgham@apple.com <bfulgham@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 19 Jun 2015 20:45:54 +0000 (20:45 +0000)
committerbfulgham@apple.com <bfulgham@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 19 Jun 2015 20:45:54 +0000 (20:45 +0000)
https://bugs.webkit.org/show_bug.cgi?id=22132

Reviewed by Zalan Bujtas.

ImageBuffer::create returns nullptr for a number of reasons, and should be
expected to do so. We missed this check in a few places, resulting in
crashes on some systems. Likewise, ImageBuffer::copyImage may return nullptr
in normal use and should be checked.

Source/WebCore:

* platform/graphics/BitmapImage.cpp:
(WebCore::BitmapImage::drawPattern): Add nullptr check for create and copyImage. Remove
extra call to 'setImageObserver'.
* platform/graphics/cairo/ImageBufferCairo.cpp:
(WebCore::ImageBuffer::drawPattern): Add nullptr check for copyImage.
* platform/graphics/cg/ImageBufferCG.cpp:
(WebCore::ImageBuffer::drawPattern): Add nullptr checks for copyImage.
* platform/graphics/filters/FETile.cpp:
(WebCore::FETile::platformApplySoftware): Add nullptr check for copyImage.
* platform/graphics/filters/FilterEffect.cpp:
(WebCore::FilterEffect::asImageBuffer): Add nullptr check for create.
(WebCore::FilterEffect::openCLImageToImageBuffer): Ditto.
* platform/graphics/texmap/BitmapTexture.cpp:
(WebCore::BitmapTexture::updateContents): Add nullptr checks for create and copyImage.
* svg/graphics/SVGImage.cpp:
(WebCore::SVGImage::drawPatternForContainer): Add nullptr check for copyImage.

Source/WebKit/mac:

* WebCoreSupport/WebContextMenuClient.mm:
(WebContextMenuClient::imageForCurrentSharingServicePickerItem): Add nullptr check
for copyImage.

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

Source/WebCore/ChangeLog
Source/WebCore/platform/graphics/BitmapImage.cpp
Source/WebCore/platform/graphics/cairo/ImageBufferCairo.cpp
Source/WebCore/platform/graphics/cg/ImageBufferCG.cpp
Source/WebCore/platform/graphics/filters/FETile.cpp
Source/WebCore/platform/graphics/filters/FilterEffect.cpp
Source/WebCore/platform/graphics/texmap/BitmapTexture.cpp
Source/WebCore/svg/graphics/SVGImage.cpp
Source/WebKit/mac/ChangeLog
Source/WebKit/mac/WebCoreSupport/WebContextMenuClient.mm

index 129985f..02e25ac 100644 (file)
@@ -1,3 +1,32 @@
+2015-06-19  Brent Fulgham  <bfulgham@apple.com>
+
+        All calls of ImageBuffer::create should null check the return value
+        https://bugs.webkit.org/show_bug.cgi?id=22132
+
+        Reviewed by Zalan Bujtas.
+
+        ImageBuffer::create returns nullptr for a number of reasons, and should be
+        expected to do so. We missed this check in a few places, resulting in
+        crashes on some systems. Likewise, ImageBuffer::copyImage may return nullptr
+        in normal use and should be checked.
+
+        * platform/graphics/BitmapImage.cpp:
+        (WebCore::BitmapImage::drawPattern): Add nullptr check for create and copyImage. Remove
+        extra call to 'setImageObserver'.
+        * platform/graphics/cairo/ImageBufferCairo.cpp:
+        (WebCore::ImageBuffer::drawPattern): Add nullptr check for copyImage.
+        * platform/graphics/cg/ImageBufferCG.cpp:
+        (WebCore::ImageBuffer::drawPattern): Add nullptr checks for copyImage.
+        * platform/graphics/filters/FETile.cpp:
+        (WebCore::FETile::platformApplySoftware): Add nullptr check for copyImage.
+        * platform/graphics/filters/FilterEffect.cpp:
+        (WebCore::FilterEffect::asImageBuffer): Add nullptr check for create.
+        (WebCore::FilterEffect::openCLImageToImageBuffer): Ditto.
+        * platform/graphics/texmap/BitmapTexture.cpp:
+        (WebCore::BitmapTexture::updateContents): Add nullptr checks for create and copyImage.
+        * svg/graphics/SVGImage.cpp:
+        (WebCore::SVGImage::drawPatternForContainer): Add nullptr check for copyImage.
+
 2015-06-19  Jeremy Jones  <jeremyj@apple.com>
 
         Get CAContext directly for CALayer instead of walking the layer tree.
index a2808e0..d8894fb 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
- * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
+ * Copyright (C) 2004, 2005, 2006, 2008, 2015 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -617,13 +617,14 @@ void BitmapImage::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect,
     }
     if (!m_cachedImage) {
         std::unique_ptr<ImageBuffer> buffer = ImageBuffer::create(expandedIntSize(tileRect.size()));
-        ASSERT(buffer.get());
+        if (!buffer)
+            return;
 
         ImageObserver* observer = imageObserver();
         ASSERT(observer);
 
         // Temporarily reset image observer, we don't want to receive any changeInRect() calls due to this relayout.
-        setImageObserver(0);
+        setImageObserver(nullptr);
 
         draw(buffer->context(), tileRect, tileRect, styleColorSpace, op, blendMode, ImageOrientationDescription());
 
@@ -631,9 +632,10 @@ void BitmapImage::drawPattern(GraphicsContext* ctxt, const FloatRect& tileRect,
         buffer->convertToLuminanceMask();
 
         m_cachedImage = buffer->copyImage(DontCopyBackingStore, Unscaled);
-        m_cachedImage->setSpaceSize(spaceSize());
+        if (!m_cachedImage)
+            return;
 
-        setImageObserver(observer);
+        m_cachedImage->setSpaceSize(spaceSize());
     }
 
     ctxt->setDrawLuminanceMask(false);
index 1fab3bd..611f4cd 100644 (file)
@@ -161,8 +161,8 @@ void ImageBuffer::draw(GraphicsContext* destinationContext, ColorSpace styleColo
 void ImageBuffer::drawPattern(GraphicsContext* context, const FloatRect& srcRect, const AffineTransform& patternTransform,
     const FloatPoint& phase, ColorSpace styleColorSpace, CompositeOperator op, const FloatRect& destRect, BlendMode)
 {
-    RefPtr<Image> image = copyImage(DontCopyBackingStore);
-    image->drawPattern(context, srcRect, patternTransform, phase, styleColorSpace, op, destRect);
+    if (RefPtr<Image> image = copyImage(DontCopyBackingStore))
+        image->drawPattern(context, srcRect, patternTransform, phase, styleColorSpace, op, destRect);
 }
 
 void ImageBuffer::platformTransformColorSpace(const Vector<int>& lookUpTable)
index f0b8047..bb9deeb 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2006 Nikolas Zimmermann <zimmermann@kde.org>
- * Copyright (C) 2008 Apple Inc. All rights reserved.
+ * Copyright (C) 2008, 2015 Apple Inc. All rights reserved.
  * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -254,15 +254,15 @@ void ImageBuffer::drawPattern(GraphicsContext* destContext, const FloatRect& src
 
     if (!context()->isAcceleratedContext()) {
         if (destContext == context() || destContext->isAcceleratedContext()) {
-            RefPtr<Image> copy = copyImage(CopyBackingStore); // Drawing into our own buffer, need to deep copy.
-            copy->drawPattern(destContext, adjustedSrcRect, patternTransform, phase, styleColorSpace, op, destRect, blendMode);
+            if (RefPtr<Image> copy = copyImage(CopyBackingStore)) // Drawing into our own buffer, need to deep copy.
+                copy->drawPattern(destContext, adjustedSrcRect, patternTransform, phase, styleColorSpace, op, destRect, blendMode);
         } else {
-            RefPtr<Image> imageForRendering = copyImage(DontCopyBackingStore);
-            imageForRendering->drawPattern(destContext, adjustedSrcRect, patternTransform, phase, styleColorSpace, op, destRect, blendMode);
+            if (RefPtr<Image> imageForRendering = copyImage(DontCopyBackingStore))
+                imageForRendering->drawPattern(destContext, adjustedSrcRect, patternTransform, phase, styleColorSpace, op, destRect, blendMode);
         }
     } else {
-        RefPtr<Image> copy = copyImage(CopyBackingStore);
-        copy->drawPattern(destContext, adjustedSrcRect, patternTransform, phase, styleColorSpace, op, destRect, blendMode);
+        if (RefPtr<Image> copy = copyImage(CopyBackingStore))
+            copy->drawPattern(destContext, adjustedSrcRect, patternTransform, phase, styleColorSpace, op, destRect, blendMode);
     }
 }
 
index f0e9b2d..dd50f63 100644 (file)
@@ -71,7 +71,11 @@ void FETile::platformApplySoftware()
     tileImageContext->translate(-inMaxEffectLocation.x(), -inMaxEffectLocation.y());
     tileImageContext->drawImageBuffer(in->asImageBuffer(), ColorSpaceDeviceRGB, in->absolutePaintRect().location());
 
-    auto pattern = Pattern::create(tileImage->copyImage(CopyBackingStore), true, true);
+    auto tileImageCopy = tileImage->copyImage(CopyBackingStore);
+    if (!tileImageCopy)
+        return;
+
+    auto pattern = Pattern::create(tileImageCopy, true, true);
 
     AffineTransform patternTransform;
     patternTransform.translate(inMaxEffectLocation.x() - maxEffectLocation.x(), inMaxEffectLocation.y() - maxEffectLocation.y());
index 50dc70d..7627008 100644 (file)
@@ -3,6 +3,7 @@
  * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
  * Copyright (C) Research In Motion Limited 2010. All rights reserved.
  * Copyright (C) 2012 University of Szeged
+ * Copyright (C) 2015 Apple Inc. All rights reserved.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -268,7 +269,7 @@ void FilterEffect::clearResultsRecursive()
 ImageBuffer* FilterEffect::asImageBuffer()
 {
     if (!hasResult())
-        return 0;
+        return nullptr;
     if (m_imageBufferResult)
         return m_imageBufferResult.get();
 #if ENABLE(OPENCL)
@@ -276,6 +277,9 @@ ImageBuffer* FilterEffect::asImageBuffer()
         return openCLImageToImageBuffer();
 #endif
     m_imageBufferResult = ImageBuffer::create(m_absolutePaintRect.size(), m_filter.filterScale(), m_resultColorSpace, m_filter.renderingMode());
+    if (!m_imageBufferResult)
+        return nullptr;
+
     IntRect destinationRect(IntPoint(), m_absolutePaintRect.size());
     if (m_premultipliedImageResult)
         m_imageBufferResult->putByteArray(Premultiplied, m_premultipliedImageResult.get(), destinationRect.size(), destinationRect, IntPoint());
@@ -291,7 +295,7 @@ ImageBuffer* FilterEffect::openCLImageToImageBuffer()
     ASSERT(context);
 
     if (context->inError())
-        return 0;
+        return nullptr;
 
     size_t origin[3] = { 0, 0, 0 };
     size_t region[3] = { m_absolutePaintRect.width(), m_absolutePaintRect.height(), 1 };
@@ -299,12 +303,15 @@ ImageBuffer* FilterEffect::openCLImageToImageBuffer()
     RefPtr<Uint8ClampedArray> destinationPixelArray = Uint8ClampedArray::create(m_absolutePaintRect.width() * m_absolutePaintRect.height() * 4);
 
     if (context->isFailed(clFinish(context->commandQueue())))
-        return 0;
+        return nullptr;
 
     if (context->isFailed(clEnqueueReadImage(context->commandQueue(), m_openCLImageResult, CL_TRUE, origin, region, 0, 0, destinationPixelArray->data(), 0, 0, 0)))
-        return 0;
+        return nullptr;
 
     m_imageBufferResult = ImageBuffer::create(m_absolutePaintRect.size());
+    if (!m_imageBufferResult)
+        return nullptr;
+
     IntRect destinationRect(IntPoint(), m_absolutePaintRect.size());
     m_imageBufferResult->putByteArray(Unmultiplied, destinationPixelArray.get(), destinationRect.size(), destinationRect, IntPoint());
 
index 42395e3..6c08258 100644 (file)
@@ -50,6 +50,8 @@ void BitmapTexture::updateContents(TextureMapper* textureMapper, GraphicsLayer*
     sourceLayer->paintGraphicsLayerContents(*context, sourceRect);
 
     RefPtr<Image> image = imageBuffer->copyImage(DontCopyBackingStore);
+    if (!image)
+        return;
 
     updateContents(image.get(), targetRect, IntPoint(), updateContentsFlag);
 }
index 41122db..de2b39c 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2006 Eric Seidel <eric@webkit.org>
- * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
+ * Copyright (C) 2008, 2009, 2015 Apple Inc. All rights reserved.
  * Copyright (C) Research In Motion Limited 2011. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -207,6 +207,8 @@ void SVGImage::drawPatternForContainer(GraphicsContext* context, const FloatSize
         buffer->convertToLuminanceMask();
 
     RefPtr<Image> image = buffer->copyImage(DontCopyBackingStore, Unscaled);
+    if (!image)
+        return;
     image->setSpaceSize(spaceSize());
 
     // Adjust the source rect and transform due to the image buffer's scaling.
index 3d78f5a..5a536b6 100644 (file)
@@ -1,3 +1,19 @@
+2015-06-19  Brent Fulgham  <bfulgham@apple.com>
+
+        All calls of ImageBuffer::create should null check the return value
+        https://bugs.webkit.org/show_bug.cgi?id=22132
+
+        Reviewed by Zalan Bujtas.
+
+        ImageBuffer::create returns nullptr for a number of reasons, and should be
+        expected to do so. We missed this check in a few places, resulting in
+        crashes on some systems. Likewise, ImageBuffer::copyImage may return nullptr
+        in normal use and should be checked.
+
+        * WebCoreSupport/WebContextMenuClient.mm:
+        (WebContextMenuClient::imageForCurrentSharingServicePickerItem): Add nullptr check
+        for copyImage.
+
 2015-06-18  Jon Lee  <jonlee@apple.com>
 
         Update AVKit usage of pip
index 60c4d63..13983ee 100644 (file)
@@ -483,6 +483,9 @@ RetainPtr<NSImage> WebContextMenuClient::imageForCurrentSharingServicePickerItem
     frameView->setPaintBehavior(oldPaintBehavior);
 
     RefPtr<Image> image = buffer->copyImage(DontCopyBackingStore);
+    if (!image)
+        return nil;
+
     return image->getNSImage();
 }
 #endif