Title: [123426] trunk
Revision
123426
Author
[email protected]
Date
2012-07-23 22:34:42 -0700 (Mon, 23 Jul 2012)

Log Message

Null-pointer crash when a derived color like -webkit-activelink is set in a gradient stop
https://bugs.webkit.org/show_bug.cgi?id=89148

Patch by Douglas Stockwell <[email protected]> on 2012-07-23
Reviewed by Simon Fraser.

Source/WebCore:

CSSGradientValue attempted to resolve colors during paint, this crashed
when a derived color like -webkit-activelink was encountered because the
corresponding element was no longer available in the StyleResolver.
Instead, by adding a field to CSSGradientColorStop we can resolve and
then cache the resolved colors at the correct time. To avoid sharing
cached derived colors between elements we clone the gradient values when
needed.

Test: fast/css/crash-on-gradient-with-derived-color.html

* css/CSSGradientValue.cpp:
(WebCore::CSSGradientValue::gradientWithStylesResolved): Added. Resolve and cache
resolved colors, clone if colors are derived from the element.
(WebCore):
(WebCore::CSSGradientValue::addStops):
(WebCore::CSSGradientValue::isCacheable): Defer to new logic in
StyleResolver.
* css/CSSGradientValue.h:
(CSSGradientColorStop): Added cache of resolved color.
(CSSGradientValue):
(CSSLinearGradientValue):
(CSSRadialGradientValue):
* css/CSSImageGeneratorValue.h:
(WebCore):
* css/CSSValue.h:
(WebCore::CSSValue::isGradientValue):
* css/StyleResolver.cpp:
(WebCore::StyleResolver::collectMatchingRulesForList):
* css/StyleResolver.h:
(StyleResolver):
* rendering/style/StyleGeneratedImage.cpp:
(WebCore::StyleGeneratedImage::image): Revert change from r96449. This
is no longer necessary as the gradient colors are now resolved at a
time when the style is set on StyleResolver.

LayoutTests:

* fast/css/crash-on-gradient-with-derived-color-expected.txt: Added.
* fast/css/crash-on-gradient-with-derived-color.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (123425 => 123426)


--- trunk/LayoutTests/ChangeLog	2012-07-24 05:19:55 UTC (rev 123425)
+++ trunk/LayoutTests/ChangeLog	2012-07-24 05:34:42 UTC (rev 123426)
@@ -1,3 +1,13 @@
+2012-07-23  Douglas Stockwell  <[email protected]>
+
+        Null-pointer crash when a derived color like -webkit-activelink is set in a gradient stop
+        https://bugs.webkit.org/show_bug.cgi?id=89148
+
+        Reviewed by Simon Fraser.
+
+        * fast/css/crash-on-gradient-with-derived-color-expected.txt: Added.
+        * fast/css/crash-on-gradient-with-derived-color.html: Added.
+
 2012-07-23  Joone Hur  <[email protected]>
 
         Unreviewed GTK/EFL gardening after r123379

Added: trunk/LayoutTests/fast/css/crash-on-gradient-with-derived-color-expected.txt (0 => 123426)


--- trunk/LayoutTests/fast/css/crash-on-gradient-with-derived-color-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/css/crash-on-gradient-with-derived-color-expected.txt	2012-07-24 05:34:42 UTC (rev 123426)
@@ -0,0 +1 @@
+This test passes if it does not CRASH.

Added: trunk/LayoutTests/fast/css/crash-on-gradient-with-derived-color.html (0 => 123426)


--- trunk/LayoutTests/fast/css/crash-on-gradient-with-derived-color.html	                        (rev 0)
+++ trunk/LayoutTests/fast/css/crash-on-gradient-with-derived-color.html	2012-07-24 05:34:42 UTC (rev 123426)
@@ -0,0 +1,7 @@
+<!DOCTYPE html>
+<script>
+if (window.layoutTestController)
+  layoutTestController.dumpAsText();
+</script>
+<body style="background-image: -webkit-linear-gradient(green, -webkit-activelink)">
+This test passes if it does not CRASH.

Modified: trunk/Source/WebCore/ChangeLog (123425 => 123426)


--- trunk/Source/WebCore/ChangeLog	2012-07-24 05:19:55 UTC (rev 123425)
+++ trunk/Source/WebCore/ChangeLog	2012-07-24 05:34:42 UTC (rev 123426)
@@ -1,3 +1,45 @@
+2012-07-23  Douglas Stockwell  <[email protected]>
+
+        Null-pointer crash when a derived color like -webkit-activelink is set in a gradient stop
+        https://bugs.webkit.org/show_bug.cgi?id=89148
+
+        Reviewed by Simon Fraser.
+
+        CSSGradientValue attempted to resolve colors during paint, this crashed
+        when a derived color like -webkit-activelink was encountered because the
+        corresponding element was no longer available in the StyleResolver.
+        Instead, by adding a field to CSSGradientColorStop we can resolve and
+        then cache the resolved colors at the correct time. To avoid sharing
+        cached derived colors between elements we clone the gradient values when
+        needed.
+
+        Test: fast/css/crash-on-gradient-with-derived-color.html
+
+        * css/CSSGradientValue.cpp:
+        (WebCore::CSSGradientValue::gradientWithStylesResolved): Added. Resolve and cache
+        resolved colors, clone if colors are derived from the element.
+        (WebCore):
+        (WebCore::CSSGradientValue::addStops):
+        (WebCore::CSSGradientValue::isCacheable): Defer to new logic in
+        StyleResolver.
+        * css/CSSGradientValue.h:
+        (CSSGradientColorStop): Added cache of resolved color.
+        (CSSGradientValue):
+        (CSSLinearGradientValue):
+        (CSSRadialGradientValue):
+        * css/CSSImageGeneratorValue.h:
+        (WebCore):
+        * css/CSSValue.h:
+        (WebCore::CSSValue::isGradientValue):
+        * css/StyleResolver.cpp:
+        (WebCore::StyleResolver::collectMatchingRulesForList):
+        * css/StyleResolver.h:
+        (StyleResolver):
+        * rendering/style/StyleGeneratedImage.cpp:
+        (WebCore::StyleGeneratedImage::image): Revert change from r96449. This
+        is no longer necessary as the gradient colors are now resolved at a
+        time when the style is set on StyleResolver.
+
 2012-07-23  Patrick Gansterer  <[email protected]>
 
         Move GregorianDateTime from JSC to WTF namespace

Modified: trunk/Source/WebCore/css/CSSGradientValue.cpp (123425 => 123426)


--- trunk/Source/WebCore/css/CSSGradientValue.cpp	2012-07-24 05:19:55 UTC (rev 123425)
+++ trunk/Source/WebCore/css/CSSGradientValue.cpp	2012-07-24 05:34:42 UTC (rev 123426)
@@ -105,6 +105,34 @@
     { }
 };
 
+PassRefPtr<CSSGradientValue> CSSGradientValue::gradientWithStylesResolved(StyleResolver* styleResolver)
+{
+    bool derived = false;
+    for (unsigned i = 0; i < m_stops.size(); i++)
+        if (styleResolver->colorFromPrimitiveValueIsDerivedFromElement(m_stops[i].m_color.get())) {
+            m_stops[i].m_colorIsDerivedFromElement = true;
+            derived = true;
+            break;
+        }
+
+    RefPtr<CSSGradientValue> result;
+    if (!derived)
+        result = this;
+    else if (isLinearGradient())
+        result = static_cast<CSSLinearGradientValue*>(this)->clone();
+    else if (isRadialGradient())
+        result = static_cast<CSSRadialGradientValue*>(this)->clone();
+    else {
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
+
+    for (unsigned i = 0; i < result->m_stops.size(); i++)
+        result->m_stops[i].m_resolvedColor = styleResolver->colorFromPrimitiveValue(result->m_stops[i].m_color.get());
+
+    return result.release();
+}
+
 void CSSGradientValue::addStops(Gradient* gradient, RenderObject* renderer, RenderStyle* rootStyle, float maxLengthForRepeat)
 {
     RenderStyle* style = renderer->style();
@@ -112,10 +140,8 @@
     if (m_deprecatedType) {
         sortStopsIfNeeded();
 
-        // We have to resolve colors.
         for (unsigned i = 0; i < m_stops.size(); i++) {
             const CSSGradientColorStop& stop = m_stops[i];
-            Color color = renderer->document()->styleResolver()->colorFromPrimitiveValue(stop.m_color.get());
 
             float offset;
             if (stop.m_position->isPercentage())
@@ -123,7 +149,7 @@
             else
                 offset = stop.m_position->getFloatValue(CSSPrimitiveValue::CSS_NUMBER);
 
-            gradient->addColorStop(offset, color);
+            gradient->addColorStop(offset, stop.m_resolvedColor);
         }
 
         // The back end already sorted the stops.
@@ -148,7 +174,7 @@
     for (size_t i = 0; i < numStops; ++i) {
         const CSSGradientColorStop& stop = m_stops[i];
 
-        stops[i].color = renderer->document()->styleResolver()->colorFromPrimitiveValue(stop.m_color.get());
+        stops[i].color = stop.m_resolvedColor;
 
         if (stop.m_position) {
             if (stop.m_position->isPercentage())
@@ -413,8 +439,7 @@
     for (size_t i = 0; i < m_stops.size(); ++i) {
         const CSSGradientColorStop& stop = m_stops[i];
 
-        CSSPrimitiveValue* color = stop.m_color.get();
-        if (color->getIdent() == CSSValueCurrentcolor)
+        if (stop.m_colorIsDerivedFromElement)
             return false;
 
         if (!stop.m_position)

Modified: trunk/Source/WebCore/css/CSSGradientValue.h (123425 => 123426)


--- trunk/Source/WebCore/css/CSSGradientValue.h	2012-07-24 05:19:55 UTC (rev 123425)
+++ trunk/Source/WebCore/css/CSSGradientValue.h	2012-07-24 05:34:42 UTC (rev 123426)
@@ -40,8 +40,11 @@
 enum CSSGradientRepeat { NonRepeating, Repeating };
 
 struct CSSGradientColorStop {
+    CSSGradientColorStop() : m_colorIsDerivedFromElement(false) { };
     RefPtr<CSSPrimitiveValue> m_position; // percentage or length
     RefPtr<CSSPrimitiveValue> m_color;
+    Color m_resolvedColor;
+    bool m_colorIsDerivedFromElement;
 };
 
 class CSSGradientValue : public CSSImageGeneratorValue {
@@ -71,6 +74,7 @@
 
     bool isPending() const { return false; }
     void loadSubimages(CachedResourceLoader*) { }
+    PassRefPtr<CSSGradientValue> gradientWithStylesResolved(StyleResolver*);
 
 protected:
     CSSGradientValue(ClassType classType, CSSGradientRepeat repeat, bool deprecatedType = false)
@@ -81,6 +85,19 @@
     {
     }
 
+    CSSGradientValue(const CSSGradientValue& other, ClassType classType, bool deprecatedType = false)
+        : CSSImageGeneratorValue(classType)
+        , m_firstX(other.m_firstX)
+        , m_firstY(other.m_firstY)
+        , m_secondX(other.m_secondX)
+        , m_secondY(other.m_secondY)
+        , m_stops(other.m_stops)
+        , m_stopsSorted(other.m_stopsSorted)
+        , m_deprecatedType(deprecatedType)
+        , m_repeating(other.isRepeating() ? Repeating : NonRepeating)
+    {
+    }
+
     void addStops(Gradient*, RenderObject*, RenderStyle* rootStyle, float maxLengthForRepeat = 0);
 
     // Resolve points/radii to front end values.
@@ -117,12 +134,23 @@
     // Create the gradient for a given size.
     PassRefPtr<Gradient> createGradient(RenderObject*, const IntSize&);
 
+    PassRefPtr<CSSLinearGradientValue> clone() const
+    {
+        return adoptRef(new CSSLinearGradientValue(*this));
+    }
+
 private:
     CSSLinearGradientValue(CSSGradientRepeat repeat, bool deprecatedType = false)
         : CSSGradientValue(LinearGradientClass, repeat, deprecatedType)
     {
     }
 
+    CSSLinearGradientValue(const CSSLinearGradientValue& other)
+        : CSSGradientValue(other, LinearGradientClass, other.deprecatedType())
+        , m_angle(other.m_angle)
+    {
+    }
+
     RefPtr<CSSPrimitiveValue> m_angle; // may be null.
 };
 
@@ -133,6 +161,11 @@
         return adoptRef(new CSSRadialGradientValue(repeat, deprecatedType));
     }
 
+    PassRefPtr<CSSRadialGradientValue> clone() const
+    {
+        return adoptRef(new CSSRadialGradientValue(*this));
+    }
+
     String customCssText() const;
 
     void setFirstRadius(PassRefPtr<CSSPrimitiveValue> val) { m_firstRadius = val; }
@@ -153,6 +186,18 @@
     {
     }
 
+    CSSRadialGradientValue(const CSSRadialGradientValue& other)
+        : CSSGradientValue(other, RadialGradientClass, other.deprecatedType())
+        , m_firstRadius(other.m_firstRadius)
+        , m_secondRadius(other.m_secondRadius)
+        , m_shape(other.m_shape)
+        , m_sizingBehavior(other.m_sizingBehavior)
+        , m_endHorizontalSize(other.m_endHorizontalSize)
+        , m_endVerticalSize(other.m_endVerticalSize)
+    {
+    }
+
+
     // Resolve points/radii to front end values.
     float resolveRadius(CSSPrimitiveValue*, RenderStyle*, RenderStyle* rootStyle, float* widthOrHeight = 0);
 

Modified: trunk/Source/WebCore/css/CSSImageGeneratorValue.h (123425 => 123426)


--- trunk/Source/WebCore/css/CSSImageGeneratorValue.h	2012-07-24 05:19:55 UTC (rev 123425)
+++ trunk/Source/WebCore/css/CSSImageGeneratorValue.h	2012-07-24 05:34:42 UTC (rev 123426)
@@ -36,6 +36,7 @@
 class CachedResourceLoader;
 class Image;
 class RenderObject;
+class StyleResolver;
 
 struct SizeAndCount {
     SizeAndCount(IntSize newSize = IntSize(), int newCount = 0)

Modified: trunk/Source/WebCore/css/CSSValue.h (123425 => 123426)


--- trunk/Source/WebCore/css/CSSValue.h	2012-07-24 05:19:55 UTC (rev 123425)
+++ trunk/Source/WebCore/css/CSSValue.h	2012-07-24 05:34:42 UTC (rev 123426)
@@ -72,6 +72,7 @@
     bool isFontFeatureValue() const { return m_classType == FontFeatureClass; }
     bool isFontValue() const { return m_classType == FontClass; }
     bool isImageGeneratorValue() const { return m_classType >= CanvasClass && m_classType <= RadialGradientClass; }
+    bool isGradientValue() const { return m_classType >= LinearGradientClass && m_classType <= RadialGradientClass; }
 #if ENABLE(CSS_IMAGE_SET)
     bool isImageSetValue() const { return m_classType == ImageSetClass; }
 #endif

Modified: trunk/Source/WebCore/css/StyleResolver.cpp (123425 => 123426)


--- trunk/Source/WebCore/css/StyleResolver.cpp	2012-07-24 05:19:55 UTC (rev 123425)
+++ trunk/Source/WebCore/css/StyleResolver.cpp	2012-07-24 05:34:42 UTC (rev 123426)
@@ -3363,7 +3363,10 @@
             for (CSSValueListIterator i = value; i.hasMore(); i.advance()) {
                 CSSValue* item = i.value();
                 if (item->isImageGeneratorValue()) {
-                    m_style->setContent(StyleGeneratedImage::create(static_cast<CSSImageGeneratorValue*>(item)), didSet);
+                    if (item->isGradientValue())
+                        m_style->setContent(StyleGeneratedImage::create(static_cast<CSSGradientValue*>(item)->gradientWithStylesResolved(this).get()), didSet);
+                    else
+                        m_style->setContent(StyleGeneratedImage::create(static_cast<CSSImageGeneratorValue*>(item)), didSet);
                     didSet = true;
 #if ENABLE(CSS_IMAGE_SET)
                 } else if (item->isImageSetValue()) {
@@ -4389,8 +4392,11 @@
     if (value->isImageValue())
         return cachedOrPendingFromValue(property, static_cast<CSSImageValue*>(value));
 
-    if (value->isImageGeneratorValue())
+    if (value->isImageGeneratorValue()) {
+        if (value->isGradientValue())
+            return generatedOrPendingFromValue(property, static_cast<CSSGradientValue*>(value)->gradientWithStylesResolved(this).get());
         return generatedOrPendingFromValue(property, static_cast<CSSImageGeneratorValue*>(value));
+    }
 
 #if ENABLE(CSS_IMAGE_SET)
     if (value->isImageSetValue())
@@ -4691,6 +4697,20 @@
     return RenderTheme::defaultTheme()->systemColor(cssValueId);
 }
 
+bool StyleResolver::colorFromPrimitiveValueIsDerivedFromElement(CSSPrimitiveValue* value)
+{
+    int ident = value->getIdent();
+    switch (ident) {
+    case CSSValueWebkitText:
+    case CSSValueWebkitLink:
+    case CSSValueWebkitActivelink:
+    case CSSValueCurrentcolor:
+        return true;
+    default:
+        return false;
+    }
+}
+
 Color StyleResolver::colorFromPrimitiveValue(CSSPrimitiveValue* value, bool forVisitedLink) const
 {
     if (value->isRGBColor())

Modified: trunk/Source/WebCore/css/StyleResolver.h (123425 => 123426)


--- trunk/Source/WebCore/css/StyleResolver.h	2012-07-24 05:19:55 UTC (rev 123425)
+++ trunk/Source/WebCore/css/StyleResolver.h	2012-07-24 05:34:42 UTC (rev 123426)
@@ -223,6 +223,7 @@
 public:
     bool useSVGZoomRules();
 
+    static bool colorFromPrimitiveValueIsDerivedFromElement(CSSPrimitiveValue*);
     Color colorFromPrimitiveValue(CSSPrimitiveValue*, bool forVisitedLink = false) const;
 
     bool hasSelectorForAttribute(const AtomicString&) const;

Modified: trunk/Source/WebCore/rendering/style/StyleGeneratedImage.cpp (123425 => 123426)


--- trunk/Source/WebCore/rendering/style/StyleGeneratedImage.cpp	2012-07-24 05:19:55 UTC (rev 123425)
+++ trunk/Source/WebCore/rendering/style/StyleGeneratedImage.cpp	2012-07-24 05:34:42 UTC (rev 123426)
@@ -85,7 +85,6 @@
 
 PassRefPtr<Image> StyleGeneratedImage::image(RenderObject* renderer, const IntSize& size) const
 {
-    renderer->document()->styleResolver()->setStyle(renderer->style());
     return m_imageGeneratorValue->image(renderer, size);
 }
 
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to