Bug 706682 - don't access bgra[4]. r=josh.
authorRafael Ávila de Espíndola <respindola@mozilla.com>
Thu, 01 Dec 2011 08:35:08 -0500
changeset 81081 4835dc9b8fc7a38fabd726dbceae68cf1a5478ed
parent 81080 b6811f220aba188ace0bdb8ccf9992531a1d26a5
child 81082 cf628f56ca799ce2635690b2681d06d31cc3d873
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersjosh
bugs706682
milestone11.0a1
Bug 706682 - don't access bgra[4]. r=josh.
dom/plugins/test/testplugin/nptest_utils.cpp
--- a/dom/plugins/test/testplugin/nptest_utils.cpp
+++ b/dom/plugins/test/testplugin/nptest_utils.cpp
@@ -80,39 +80,32 @@ doubleVariantToIdentifier(NPVariant vari
   assert(NPVARIANT_IS_DOUBLE(variant));
   double value = NPVARIANT_TO_DOUBLE(variant);
   // sadly there is no "getdoubleidentifier"
   int32_t integer = static_cast<int32_t>(value);
   return NPN_GetIntIdentifier(integer);
 }
 
 /*
- * Parse a color in hex format, like AARRGGBB
- * If the string is short, portions to the left are assumed omitted.
- * R G B default to 0, A defaults to 0xFF
+ * Parse a color in hex format, #AARRGGBB or AARRGGBB.
  */
 PRUint32
 parseHexColor(const char* color, int len)
 {
   PRUint8 bgra[4] = { 0, 0, 0, 0xFF };
   int i = 0;
 
+  assert(len == 9 || len == 8);
+
   // start from the right and work to the left
-  while (len > 0) {
+  while (len >= 2) { // we have at least #AA or AA left.
     char byte[3];
-    if (len > 1) {
-      // parse two hex digits
-      byte[0] = color[len - 2];
-      byte[1] = color[len - 1];
-    }
-    else {
-      // only one digit left
-      byte[0] = '0';
-      byte[1] = color[len - 1];
-    }
+    // parse two hex digits
+    byte[0] = color[len - 2];
+    byte[1] = color[len - 1];
     byte[2] = '\0';
 
     bgra[i] = (PRUint8)(strtoul(byte, NULL, 16) & 0xFF);
     i++;
     len -= 2;
   }
   return (bgra[3] << 24) | (bgra[2] << 16) | (bgra[1] << 8) | bgra[0];
 }