Blame SOURCES/rhbz-1956868.patch

07a7da
diff -up libwebp-1.0.0/src/dec/vp8l_dec.c.old libwebp-1.0.0/src/dec/vp8l_dec.c
07a7da
--- libwebp-1.0.0/src/dec/vp8l_dec.c.old	2018-04-21 05:04:55.000000000 +0200
07a7da
+++ libwebp-1.0.0/src/dec/vp8l_dec.c	2018-11-09 00:29:51.000000000 +0100
07a7da
@@ -362,12 +362,19 @@ static int ReadHuffmanCodes(VP8LDecoder*
07a7da
   VP8LMetadata* const hdr = &dec->hdr_;
07a7da
   uint32_t* huffman_image = NULL;
07a7da
   HTreeGroup* htree_groups = NULL;
07a7da
+  // When reading htrees, some might be unused, as the format allows it.
07a7da
+  // We will still read them but put them in this htree_group_bogus.
07a7da
+  HTreeGroup htree_group_bogus;
07a7da
   HuffmanCode* huffman_tables = NULL;
07a7da
+  HuffmanCode* huffman_tables_bogus = NULL;
07a7da
   HuffmanCode* next = NULL;
07a7da
   int num_htree_groups = 1;
07a7da
+  int num_htree_groups_max = 1;
07a7da
   int max_alphabet_size = 0;
07a7da
   int* code_lengths = NULL;
07a7da
   const int table_size = kTableSize[color_cache_bits];
07a7da
+  int* mapping = NULL;
07a7da
+  int ok = 0;
07a7da
 
07a7da
   if (allow_recursion && VP8LReadBits(br, 1)) {
07a7da
     // use meta Huffman codes.
07a7da
@@ -384,10 +391,42 @@ static int ReadHuffmanCodes(VP8LDecoder*
07a7da
       // The huffman data is stored in red and green bytes.
07a7da
       const int group = (huffman_image[i] >> 8) & 0xffff;
07a7da
       huffman_image[i] = group;
07a7da
-      if (group >= num_htree_groups) {
07a7da
-        num_htree_groups = group + 1;
07a7da
+      if (group >= num_htree_groups_max) {
07a7da
+        num_htree_groups_max = group + 1;
07a7da
       }
07a7da
     }
07a7da
+    // Check the validity of num_htree_groups_max. If it seems too big, use a
07a7da
+    // smaller value for later. This will prevent big memory allocations to end
07a7da
+    // up with a bad bitstream anyway.
07a7da
+    // The value of 1000 is totally arbitrary. We know that num_htree_groups_max
07a7da
+    // is smaller than (1 << 16) and should be smaller than the number of pixels
07a7da
+    // (though the format allows it to be bigger).
07a7da
+    if (num_htree_groups_max > 1000 || num_htree_groups_max > xsize * ysize) {
07a7da
+      // Create a mapping from the used indices to the minimal set of used
07a7da
+      // values [0, num_htree_groups)
07a7da
+      mapping = (int*)WebPSafeMalloc(num_htree_groups_max, sizeof(*mapping));
07a7da
+      if (mapping == NULL) {
07a7da
+        dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
07a7da
+        goto Error;
07a7da
+      }
07a7da
+      // -1 means a value is unmapped, and therefore unused in the Huffman
07a7da
+      // image.
07a7da
+      memset(mapping, 0xff, num_htree_groups_max * sizeof(*mapping));
07a7da
+      for (num_htree_groups = 0, i = 0; i < huffman_pixs; ++i) {
07a7da
+        // Get the current mapping for the group and remap the Huffman image.
07a7da
+        int* const mapped_group = &mapping[huffman_image[i]];
07a7da
+        if (*mapped_group == -1) *mapped_group = num_htree_groups++;
07a7da
+        huffman_image[i] = *mapped_group;
07a7da
+      }
07a7da
+      huffman_tables_bogus = (HuffmanCode*)WebPSafeMalloc(
07a7da
+          table_size, sizeof(*huffman_tables_bogus));
07a7da
+      if (huffman_tables_bogus == NULL) {
07a7da
+        dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
07a7da
+        goto Error;
07a7da
+      }
07a7da
+    } else {
07a7da
+      num_htree_groups = num_htree_groups_max;
07a7da
+    }
07a7da
   }
07a7da
 
07a7da
   if (br->eos_) goto Error;
07a7da
@@ -403,11 +442,11 @@ static int ReadHuffmanCodes(VP8LDecoder*
07a7da
     }
07a7da
   }
07a7da
 
07a7da
+  code_lengths = (int*)WebPSafeCalloc((uint64_t)max_alphabet_size,
07a7da
+                                      sizeof(*code_lengths));
07a7da
   huffman_tables = (HuffmanCode*)WebPSafeMalloc(num_htree_groups * table_size,
07a7da
                                                 sizeof(*huffman_tables));
07a7da
   htree_groups = VP8LHtreeGroupsNew(num_htree_groups);
07a7da
-  code_lengths = (int*)WebPSafeCalloc((uint64_t)max_alphabet_size,
07a7da
-                                      sizeof(*code_lengths));
07a7da
 
07a7da
   if (htree_groups == NULL || code_lengths == NULL || huffman_tables == NULL) {
07a7da
     dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
07a7da
@@ -415,28 +454,35 @@ static int ReadHuffmanCodes(VP8LDecoder*
07a7da
   }
07a7da
 
07a7da
   next = huffman_tables;
07a7da
-  for (i = 0; i < num_htree_groups; ++i) {
07a7da
-    HTreeGroup* const htree_group = &htree_groups[i];
07a7da
+  for (i = 0; i < num_htree_groups_max; ++i) {
07a7da
+    // If the index "i" is unused in the Huffman image, read the coefficients
07a7da
+    // but store them to a bogus htree_group.
07a7da
+    const int is_bogus = (mapping != NULL && mapping[i] == -1);
07a7da
+    HTreeGroup* const htree_group =
07a7da
+        is_bogus ? &htree_group_bogus :
07a7da
+        &htree_groups[(mapping == NULL) ? i : mapping[i]];
07a7da
     HuffmanCode** const htrees = htree_group->htrees;
07a7da
+    HuffmanCode* huffman_tables_i = is_bogus ? huffman_tables_bogus : next;
07a7da
     int size;
07a7da
     int total_size = 0;
07a7da
     int is_trivial_literal = 1;
07a7da
     int max_bits = 0;
07a7da
     for (j = 0; j < HUFFMAN_CODES_PER_META_CODE; ++j) {
07a7da
       int alphabet_size = kAlphabetSize[j];
07a7da
-      htrees[j] = next;
07a7da
+      htrees[j] = huffman_tables_i;
07a7da
       if (j == 0 && color_cache_bits > 0) {
07a7da
         alphabet_size += 1 << color_cache_bits;
07a7da
       }
07a7da
-      size = ReadHuffmanCode(alphabet_size, dec, code_lengths, next);
07a7da
+      size =
07a7da
+          ReadHuffmanCode(alphabet_size, dec, code_lengths, huffman_tables_i);
07a7da
       if (size == 0) {
07a7da
         goto Error;
07a7da
       }
07a7da
       if (is_trivial_literal && kLiteralMap[j] == 1) {
07a7da
-        is_trivial_literal = (next->bits == 0);
07a7da
+        is_trivial_literal = (huffman_tables_i->bits == 0);
07a7da
       }
07a7da
-      total_size += next->bits;
07a7da
-      next += size;
07a7da
+      total_size += huffman_tables_i->bits;
07a7da
+      huffman_tables_i += size;
07a7da
       if (j <= ALPHA) {
07a7da
         int local_max_bits = code_lengths[0];
07a7da
         int k;
07a7da
@@ -448,38 +494,41 @@ static int ReadHuffmanCodes(VP8LDecoder*
07a7da
         max_bits += local_max_bits;
07a7da
       }
07a7da
     }
07a7da
+    if (!is_bogus) next = huffman_tables_i;
07a7da
     htree_group->is_trivial_literal = is_trivial_literal;
07a7da
     htree_group->is_trivial_code = 0;
07a7da
     if (is_trivial_literal) {
07a7da
       const int red = htrees[RED][0].value;
07a7da
       const int blue = htrees[BLUE][0].value;
07a7da
       const int alpha = htrees[ALPHA][0].value;
07a7da
-      htree_group->literal_arb =
07a7da
-          ((uint32_t)alpha << 24) | (red << 16) | blue;
07a7da
+      htree_group->literal_arb = ((uint32_t)alpha << 24) | (red << 16) | blue;
07a7da
       if (total_size == 0 && htrees[GREEN][0].value < NUM_LITERAL_CODES) {
07a7da
         htree_group->is_trivial_code = 1;
07a7da
         htree_group->literal_arb |= htrees[GREEN][0].value << 8;
07a7da
       }
07a7da
     }
07a7da
-    htree_group->use_packed_table = !htree_group->is_trivial_code &&
07a7da
-                                    (max_bits < HUFFMAN_PACKED_BITS);
07a7da
+    htree_group->use_packed_table =
07a7da
+        !htree_group->is_trivial_code && (max_bits < HUFFMAN_PACKED_BITS);
07a7da
     if (htree_group->use_packed_table) BuildPackedTable(htree_group);
07a7da
   }
07a7da
-  WebPSafeFree(code_lengths);
07a7da
+  ok = 1;
07a7da
 
07a7da
-  // All OK. Finalize pointers and return.
07a7da
+  // All OK. Finalize pointers.
07a7da
   hdr->huffman_image_ = huffman_image;
07a7da
   hdr->num_htree_groups_ = num_htree_groups;
07a7da
   hdr->htree_groups_ = htree_groups;
07a7da
   hdr->huffman_tables_ = huffman_tables;
07a7da
-  return 1;
07a7da
 
07a7da
  Error:
07a7da
   WebPSafeFree(code_lengths);
07a7da
-  WebPSafeFree(huffman_image);
07a7da
-  WebPSafeFree(huffman_tables);
07a7da
-  VP8LHtreeGroupsFree(htree_groups);
07a7da
-  return 0;
07a7da
+  WebPSafeFree(huffman_tables_bogus);
07a7da
+  WebPSafeFree(mapping);
07a7da
+  if (!ok) {
07a7da
+    WebPSafeFree(huffman_image);
07a7da
+    WebPSafeFree(huffman_tables);
07a7da
+    VP8LHtreeGroupsFree(htree_groups);
07a7da
+  }
07a7da
+  return ok;
07a7da
 }
07a7da
 
07a7da
 //------------------------------------------------------------------------------
07a7da
@@ -884,7 +933,11 @@ static WEBP_INLINE void CopyBlock8b(uint
07a7da
 #endif
07a7da
         break;
07a7da
       case 2:
07a7da
+#if !defined(WORDS_BIGENDIAN)
07a7da
         memcpy(&pattern, src, sizeof(uint16_t));
07a7da
+#else
07a7da
+        pattern = ((uint32_t)src[0] << 8) | src[1];
07a7da
+#endif
07a7da
 #if defined(__arm__) || defined(_M_ARM)
07a7da
         pattern |= pattern << 16;
07a7da
 #elif defined(WEBP_USE_MIPS_DSP_R2)
07a7da
@@ -1523,7 +1576,6 @@ int VP8LDecodeAlphaHeader(ALPHDecoder* c
07a7da
   if (dec == NULL) return 0;
07a7da
 
07a7da
   assert(alph_dec != NULL);
07a7da
-  alph_dec->vp8l_dec_ = dec;
07a7da
 
07a7da
   dec->width_ = alph_dec->width_;
07a7da
   dec->height_ = alph_dec->height_;
07a7da
@@ -1555,11 +1607,12 @@ int VP8LDecodeAlphaHeader(ALPHDecoder* c
07a7da
 
07a7da
   if (!ok) goto Err;
07a7da
 
07a7da
+  // Only set here, once we are sure it is valid (to avoid thread races).
07a7da
+  alph_dec->vp8l_dec_ = dec;
07a7da
   return 1;
07a7da
 
07a7da
  Err:
07a7da
-  VP8LDelete(alph_dec->vp8l_dec_);
07a7da
-  alph_dec->vp8l_dec_ = NULL;
07a7da
+  VP8LDelete(dec);
07a7da
   return 0;
07a7da
 }
07a7da