Bug 1117406 - Fix handling of out-of-range PNG tRNS values. r=jmuizelaar, a=abillings
authorGlenn Randers-Pehrson <glennrp+bmo@gmail.com>
Mon, 26 Jan 2015 16:07:00 -0500
changeset 200535 5ee3807b4bb24c5fef5d4262c05749118a98943c
parent 200534 5d4bc3da7c283414df1a52e81b3300883a79e2c6
child 200536 bbab000f050730dc16d4d262783a66e5bbb9021b
push id177
push userryanvm@gmail.com
push dateMon, 26 Jan 2015 21:07:03 +0000
treeherdermozilla-esr31@5ee3807b4bb2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjmuizelaar, abillings
bugs1117406
milestone31.4.0
Bug 1117406 - Fix handling of out-of-range PNG tRNS values. r=jmuizelaar, a=abillings
image/decoders/nsPNGDecoder.cpp
--- a/image/decoders/nsPNGDecoder.cpp
+++ b/image/decoders/nsPNGDecoder.cpp
@@ -523,34 +523,36 @@ nsPNGDecoder::info_callback(png_structp 
 
   if (color_type == PNG_COLOR_TYPE_PALETTE)
     png_set_expand(png_ptr);
 
   if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
     png_set_expand(png_ptr);
 
   if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
-    int sample_max = (1 << bit_depth);
     png_color_16p trans_values;
     png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &trans_values);
     /* libpng doesn't reject a tRNS chunk with out-of-range samples
        so we check it here to avoid setting up a useless opacity
-       channel or producing unexpected transparent pixels when using
-       libpng-1.2.19 through 1.2.26 (bug #428045) */
-    if ((color_type == PNG_COLOR_TYPE_GRAY &&
-       (int)trans_values->gray > sample_max) ||
-       (color_type == PNG_COLOR_TYPE_RGB &&
-       ((int)trans_values->red > sample_max ||
-       (int)trans_values->green > sample_max ||
-       (int)trans_values->blue > sample_max)))
+       channel or producing unexpected transparent pixels (bug #428045) */
+    if (bit_depth < 16) {
+      png_uint_16 sample_max = (1 << bit_depth) - 1;
+      if ((color_type == PNG_COLOR_TYPE_GRAY &&
+           trans_values->gray > sample_max) ||
+           (color_type == PNG_COLOR_TYPE_RGB &&
+           (trans_values->red > sample_max ||
+           trans_values->green > sample_max ||
+           trans_values->blue > sample_max)))
       {
         /* clear the tRNS valid flag and release tRNS memory */
         png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
+        num_trans = 0;
       }
-    else
+    }
+    if (num_trans != 0)
       png_set_expand(png_ptr);
   }
 
   if (bit_depth == 16)
     png_set_scale_16(png_ptr);
 
   qcms_data_type inType = QCMS_DATA_RGBA_8;
   uint32_t intent = -1;