[clutter] color: Validate hex formats



commit 0550a8b99d0589ef9a196c6787577a0975014f3f
Author: Emmanuele Bassi <ebassi linux intel com>
Date:   Thu Nov 10 14:13:45 2011 +0000

    color: Validate hex formats
    
    Right now, we pass through to Pango unrecognized hexadecimal formats
    when parsing colors from strings. Since we parse all possible formats
    ourselves, we can do validation ourselves as well, and avoid the Pango
    path.

 clutter/clutter-color.c |   26 +++++++++++++++-----------
 1 files changed, 15 insertions(+), 11 deletions(-)
---
diff --git a/clutter/clutter-color.c b/clutter/clutter-color.c
index 17b8b3c..cef0109 100644
--- a/clutter/clutter-color.c
+++ b/clutter/clutter-color.c
@@ -720,17 +720,16 @@ clutter_color_from_string (ClutterColor *color,
    * parsing the color ourselves, as we need the alpha channel that
    * Pango can't retrieve.
    */
-  if (str[0] == '#')
+  if (str[0] == '#' && str[1] != '\0')
     {
+      gsize length = strlen (str + 1);
       gint32 result;
 
-      if (sscanf (str + 1, "%x", &result))
+      if (sscanf (str + 1, "%x", &result) == 1)
         {
-          gsize length = strlen (str);
-
           switch (length)
             {
-            case 9: /* rrggbbaa */
+            case 8: /* rrggbbaa */
               color->red   = (result >> 24) & 0xff;
               color->green = (result >> 16) & 0xff;
               color->blue  = (result >>  8) & 0xff;
@@ -739,7 +738,7 @@ clutter_color_from_string (ClutterColor *color,
 
               return TRUE;
 
-            case 7: /* #rrggbb */
+            case 6: /* #rrggbb */
               color->red   = (result >> 16) & 0xff;
               color->green = (result >>  8) & 0xff;
               color->blue  = result & 0xff;
@@ -748,7 +747,7 @@ clutter_color_from_string (ClutterColor *color,
 
               return TRUE;
 
-            case 5: /* #rgba */
+            case 4: /* #rgba */
               color->red   = ((result >> 12) & 0xf);
               color->green = ((result >>  8) & 0xf);
               color->blue  = ((result >>  4) & 0xf);
@@ -761,7 +760,7 @@ clutter_color_from_string (ClutterColor *color,
 
               return TRUE;
 
-            case 4: /* #rgb */
+            case 3: /* #rgb */
               color->red   = ((result >>  8) & 0xf);
               color->green = ((result >>  4) & 0xf);
               color->blue  = result & 0xf;
@@ -775,13 +774,18 @@ clutter_color_from_string (ClutterColor *color,
               return TRUE;
 
             default:
-              /* pass through to Pango */
-              break;
+              return FALSE;
             }
         }
     }
 
-  /* Fall back to pango for named colors */
+  /* fall back to pango for X11-style named colors; see:
+   *
+   *   http://en.wikipedia.org/wiki/X11_color_names
+   *
+   * for a list. at some point we might even ship with our own list generated
+   * from X11/rgb.txt, like we generate the key symbols.
+   */
   if (pango_color_parse (&pango_color, str))
     {
       color->red   = pango_color.red;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]