Blame SOURCES/rhbz-1956868.patch

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