summaryrefslogtreecommitdiff
path: root/third_party/libopenjpeg20/0029-avoid-division-by-0.patch
blob: 4ea2b7ce3c19f8d16bb24a3540ab2085577f3b16 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
diff --git a/third_party/libopenjpeg20/pi.c b/third_party/libopenjpeg20/pi.c
index 9097e31a0..083674222 100644
--- a/third_party/libopenjpeg20/pi.c
+++ b/third_party/libopenjpeg20/pi.c
@@ -355,12 +355,20 @@ if (!pi->tp_on){
                                        }
                                        res = &comp->resolutions[pi->resno];
                                        levelno = comp->numresolutions - 1 - pi->resno;
-                                       trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
-                                       try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
-                                       trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
-                                       try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
+                                       OPJ_INT32 x_divisor = comp->dx << levelno;
+                                       OPJ_INT32 y_divisor = comp->dy << levelno;
+                                       if (x_divisor == 0 || y_divisor == 0) {
+                                               continue;
+                                       }
+                                       trx0 = opj_int_ceildiv(pi->tx0, x_divisor);
+                                       try0 = opj_int_ceildiv(pi->ty0, y_divisor);
+                                       trx1 = opj_int_ceildiv(pi->tx1, x_divisor);
+                                       try1 = opj_int_ceildiv(pi->ty1, y_divisor);
                                        rpx = res->pdx + levelno;
                                        rpy = res->pdy + levelno;
+                                       if (comp->dy << rpy == 0 || 1 << rpy == 0 || comp->dx << rpx == 0 || 1 << rpx == 0) {
+                                               continue;
+                                       }
                                        if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
                                                continue;       
                                        }
@@ -372,9 +380,9 @@ if (!pi->tp_on){
                                        
                                        if ((trx0==trx1)||(try0==try1)) continue;
                                        
-                                       prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
+                                       prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, x_divisor), (OPJ_INT32)res->pdx)
                                                 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
-                                       prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
+                                       prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, y_divisor), (OPJ_INT32)res->pdy)
                                                 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
                                        pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
                                        if (pi->precno >= res->pw * res->ph) {
@@ -439,12 +447,20 @@ static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
                                        OPJ_INT32 prci, prcj;
                                        res = &comp->resolutions[pi->resno];
                                        levelno = comp->numresolutions - 1 - pi->resno;
-                                       trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
-                                       try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
-                                       trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
-                                       try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
+                                       OPJ_INT32 x_divisor = comp->dx << levelno;
+                                       OPJ_INT32 y_divisor = comp->dy << levelno;
+                                       if (x_divisor == 0 || y_divisor == 0) {
+                                               continue;
+                                       }
+                                       trx0 = opj_int_ceildiv(pi->tx0, x_divisor);
+                                       try0 = opj_int_ceildiv(pi->ty0, y_divisor);
+                                       trx1 = opj_int_ceildiv(pi->tx1, x_divisor);
+                                       try1 = opj_int_ceildiv(pi->ty1, y_divisor);
                                        rpx = res->pdx + levelno;
                                        rpy = res->pdy + levelno;
+                                       if (comp->dy << rpy == 0 || 1 << rpy == 0 || comp->dx << rpx == 0 || 1 << rpx == 0) {
+                                               continue;
+                                       }
                                        if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
                                                continue;       
                                        }
@@ -456,9 +472,9 @@ static OPJ_BOOL opj_pi_next_pcrl(opj_pi_iterator_t * pi) {
                                        
                                        if ((trx0==trx1)||(try0==try1)) continue;
                                        
-                                       prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
+                                       prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, x_divisor), (OPJ_INT32)res->pdx)
                                                 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
-                                       prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
+                                       prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, y_divisor), (OPJ_INT32)res->pdy)
                                                 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
                                        pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
                                        if (pi->precno >= res->pw * res->ph) {
@@ -521,26 +537,33 @@ static OPJ_BOOL opj_pi_next_cprl(opj_pi_iterator_t * pi) {
                                        OPJ_INT32 prci, prcj;
                                        res = &comp->resolutions[pi->resno];
                                        levelno = comp->numresolutions - 1 - pi->resno;
-                                       trx0 = opj_int_ceildiv(pi->tx0, (OPJ_INT32)(comp->dx << levelno));
-                                       try0 = opj_int_ceildiv(pi->ty0, (OPJ_INT32)(comp->dy << levelno));
-                                       trx1 = opj_int_ceildiv(pi->tx1, (OPJ_INT32)(comp->dx << levelno));
-                                       try1 = opj_int_ceildiv(pi->ty1, (OPJ_INT32)(comp->dy << levelno));
+                                       OPJ_INT32 x_divisor = comp->dx << levelno;
+                                       OPJ_INT32 y_divisor = comp->dy << levelno;
+                                       if (x_divisor == 0 || y_divisor == 0) {
+                                               continue;
+                                       }
+                                       trx0 = opj_int_ceildiv(pi->tx0, x_divisor);
+                                       try0 = opj_int_ceildiv(pi->ty0, y_divisor);
+                                       trx1 = opj_int_ceildiv(pi->tx1, x_divisor);
+                                       try1 = opj_int_ceildiv(pi->ty1, y_divisor);
                                        rpx = res->pdx + levelno;
                                        rpy = res->pdy + levelno;
+                                       if (comp->dy << rpy == 0 || 1 << rpy == 0 || comp->dx << rpx == 0 || 1 << rpx == 0) {
+                                               continue;
+                                       }
                                        if (!((pi->y % (OPJ_INT32)(comp->dy << rpy) == 0) || ((pi->y == pi->ty0) && ((try0 << levelno) % (1 << rpy))))){
                                                continue;       
                                        }
                                        if (!((pi->x % (OPJ_INT32)(comp->dx << rpx) == 0) || ((pi->x == pi->tx0) && ((trx0 << levelno) % (1 << rpx))))){
                                                continue;
                                        }
-                                       
                                        if ((res->pw==0)||(res->ph==0)) continue;
                                        
                                        if ((trx0==trx1)||(try0==try1)) continue;
                                        
-                                       prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, (OPJ_INT32)(comp->dx << levelno)), (OPJ_INT32)res->pdx)
+                                       prci = opj_int_floordivpow2(opj_int_ceildiv(pi->x, x_divisor), (OPJ_INT32)res->pdx)
                                                 - opj_int_floordivpow2(trx0, (OPJ_INT32)res->pdx);
-                                       prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, (OPJ_INT32)(comp->dy << levelno)), (OPJ_INT32)res->pdy)
+                                       prcj = opj_int_floordivpow2(opj_int_ceildiv(pi->y, y_divisor), (OPJ_INT32)res->pdy)
                                                 - opj_int_floordivpow2(try0, (OPJ_INT32)res->pdy);
                                        pi->precno = (OPJ_UINT32)(prci + prcj * (OPJ_INT32)res->pw);
                                        if (pi->precno >= res->pw * res->ph) {