changeset 693:478dfc078e12

make StyleEngine::computeValue() and computeLength() return bool StyleEngine::computeValue now returns whether the value was actually set. This information is now used in computeLength to avoid setting of random length values in case of an unrecognized CSS_LENGTH_TYPE. This fixes crashes with images that have invalid width or height attributes (e.g. <img src="foo.jpg" width="10px" />).
author Johannes Hofmann <Johannes.Hofmann@gmx.de>
date Fri, 19 Dec 2008 18:32:16 +0100
parents 920b9f4cabf3
children 59a1c50de390
files src/styleengine.cc src/styleengine.hh
diffstat 2 files changed, 24 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/styleengine.cc	Wed Dec 17 17:51:12 2008 +0100
+++ b/src/styleengine.cc	Fri Dec 19 18:32:16 2008 +0100
@@ -329,7 +329,7 @@
 /**
  * \brief Resolve relative lengths to absolute values.
  */
-void StyleEngine::computeValue (int *dest, CssLength value, Font *font) {
+bool StyleEngine::computeValue (int *dest, CssLength value, Font *font) {
    static float dpmm;
 
    if (dpmm == 0.0)
@@ -338,39 +338,45 @@
    switch (CSS_LENGTH_TYPE (value)) {
       case CSS_LENGTH_TYPE_PX:
          *dest = (int) CSS_LENGTH_VALUE (value);
-         break;
+         return true;
       case CSS_LENGTH_TYPE_MM:
          *dest = (int) (CSS_LENGTH_VALUE (value) * dpmm);
-         break;
+        return true;
       case CSS_LENGTH_TYPE_EM:
          *dest = (int) (CSS_LENGTH_VALUE (value) * font->size);
-         break;
+        return true;
       case CSS_LENGTH_TYPE_EX:
          *dest = (int) (CSS_LENGTH_VALUE(value) * font->xHeight);
-         break;
+        return true;
       default:
          break;
    }
+
+   return false;
 }
 
-void StyleEngine::computeValue (int *dest, CssLength value, Font *font,
+bool StyleEngine::computeValue (int *dest, CssLength value, Font *font,
                                 int percentageBase) {
-   if (CSS_LENGTH_TYPE (value) == CSS_LENGTH_TYPE_PERCENTAGE)
+   if (CSS_LENGTH_TYPE (value) == CSS_LENGTH_TYPE_PERCENTAGE) {
       *dest = (int) (CSS_LENGTH_VALUE (value) * percentageBase);
-   else
-      computeValue (dest, value, font);
+      return true;
+   } else
+      return computeValue (dest, value, font);
 }
 
-void StyleEngine::computeLength (dw::core::style::Length *dest,
+bool StyleEngine::computeLength (dw::core::style::Length *dest,
                                  CssLength value, Font *font) {
    int v;
 
-   if (CSS_LENGTH_TYPE (value) == CSS_LENGTH_TYPE_PERCENTAGE)
+   if (CSS_LENGTH_TYPE (value) == CSS_LENGTH_TYPE_PERCENTAGE) {
       *dest = createPerLength (CSS_LENGTH_VALUE (value));
-    else {
-      computeValue (&v, value, font);
-      *dest = createAbsLength (v);
-   }
+      return true;
+    } else if (computeValue (&v, value, font)) {
+       *dest = createAbsLength (v);
+       return true;
+    }
+
+   return false;
 }
 
 /**
--- a/src/styleengine.hh	Wed Dec 17 17:51:12 2008 +0100
+++ b/src/styleengine.hh	Fri Dec 19 18:32:16 2008 +0100
@@ -23,10 +23,10 @@
       dw::core::style::Style *style0 (CssPropertyList *nonCssHints = NULL);
       dw::core::style::Style *wordStyle0 (CssPropertyList *nonCssHints = NULL);
       void apply (dw::core::style::StyleAttrs *attrs, CssPropertyList *props);
-      void computeValue (int *dest, CssLength value, dw::core::style::Font *font);
-      void computeValue (int *dest, CssLength value, dw::core::style::Font *font,
+      bool computeValue (int *dest, CssLength value, dw::core::style::Font *font);
+      bool computeValue (int *dest, CssLength value, dw::core::style::Font *font,
          int percentageBase);
-      void computeLength (dw::core::style::Length *dest, CssLength value, dw::core::style::Font *font);
+      bool computeLength (dw::core::style::Length *dest, CssLength value, dw::core::style::Font *font);
 
    public:
       StyleEngine (dw::core::Layout *layout);