summaryrefslogtreecommitdiffstats
path: root/media/libjpeg/1050342.diff
blob: 592fe5ed5765e3c51cab79077293d0776d68cf23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Bug 1050342. Fix a case where the fast huffman decoder in libjpeg-turbo can produce different results depending on how data is fed to it.

This change comes from the blink repo https://codereview.appspot.com/229430043/ and is unlikely to be accepted upstream into libjpeg-turbo.

diff --git jdhuff.c jdhuff.c
--- jdhuff.c
+++ jdhuff.c
@@ -674,9 +674,9 @@ decode_mcu_fast(j_decompress_ptr cinfo, 
     d_derived_tbl *dctbl = entropy->dc_cur_tbls[blkn];
     d_derived_tbl *actbl = entropy->ac_cur_tbls[blkn];
     register int s, k, r, l;
 
-    HUFF_DECODE_FAST(s, l, dctbl);
+    HUFF_DECODE_FAST(s, l, dctbl, slow_decode_mcu);
     if (s) {
       FILL_BIT_BUFFER_FAST
       r = GET_BITS(s);
       s = HUFF_EXTEND(r, s);
@@ -692,9 +692,9 @@ decode_mcu_fast(j_decompress_ptr cinfo, 
 
     if (entropy->ac_needed[blkn] && block) {
 
       for (k = 1; k < DCTSIZE2; k++) {
-        HUFF_DECODE_FAST(s, l, actbl);
+        HUFF_DECODE_FAST(s, l, actbl, slow_decode_mcu);
         r = s >> 4;
         s &= 15;
 
         if (s) {
@@ -711,9 +711,9 @@ decode_mcu_fast(j_decompress_ptr cinfo, 
 
     } else {
 
       for (k = 1; k < DCTSIZE2; k++) {
-        HUFF_DECODE_FAST(s, l, actbl);
+        HUFF_DECODE_FAST(s, l, actbl, slow_decode_mcu);
         r = s >> 4;
         s &= 15;
 
         if (s) {
@@ -728,8 +728,9 @@ decode_mcu_fast(j_decompress_ptr cinfo, 
     }
   }
 
   if (cinfo->unread_marker != 0) {
+slow_decode_mcu:
     cinfo->unread_marker = 0;
     return FALSE;
   }
 
diff --git jdhuff.h jdhuff.h
--- jdhuff.h
+++ jdhuff.h
@@ -210,9 +210,9 @@ slowlabel: \
     get_buffer = state.get_buffer;  bits_left = state.bits_left; \
   } \
 }
 
-#define HUFF_DECODE_FAST(s, nb, htbl) \
+#define HUFF_DECODE_FAST(s, nb, htbl, slowlabel) \
   FILL_BIT_BUFFER_FAST; \
   s = PEEK_BITS(HUFF_LOOKAHEAD); \
   s = htbl->lookup[s]; \
   nb = s >> HUFF_LOOKAHEAD; \
@@ -227,9 +227,11 @@ slowlabel: \
       s <<= 1; \
       s |= GET_BITS(1); \
       nb++; \
     } \
-    s = htbl->pub->huffval[(int)(s + htbl->valoffset[nb]) & 0xFF]; \
+    if (nb > 16) \
+      goto slowlabel; \
+    s = htbl->pub->huffval[ (int) (s + htbl->valoffset[nb]) ]; \
   }
 
 /* Out-of-line case for Huffman code fetching */
 EXTERN(int) jpeg_huff_decode(bitread_working_state *state,