summaryrefslogtreecommitdiff
path: root/third_party/agg23/0003-ubsan-render-line-error.patch
blob: 7bd3b70424d7174dc8e23224954bdaa4128740c3 (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
diff --git a/third_party/agg23/agg_rasterizer_scanline_aa.cpp b/third_party/agg23/agg_rasterizer_scanline_aa.cpp
index 46379f6..c6b3f01 100644
--- a/third_party/agg23/agg_rasterizer_scanline_aa.cpp
+++ b/third_party/agg23/agg_rasterizer_scanline_aa.cpp
@@ -48,6 +48,7 @@
 //----------------------------------------------------------------------------
 #include <limits.h>
 #include "agg_rasterizer_scanline_aa.h"
+#include "third_party/base/numerics/safe_math.h"
 namespace agg
 {
 AGG_INLINE void cell_aa::set_cover(int c, int a)
@@ -237,7 +238,7 @@ void outline_aa::render_line(int x1, int y1, int x2, int y2)
     int fy1 = y1 & poly_base_mask;
     int fy2 = y2 & poly_base_mask;
     int x_from, x_to;
-    int p, rem, mod, lift, delta, first, incr;
+    int rem, mod, lift, delta, first, incr;
     if(ey1 == ey2) {
         render_hline(ey1, x1, fy1, x2, fy2);
         return;
@@ -268,16 +269,22 @@ void outline_aa::render_line(int x1, int y1, int x2, int y2)
         m_cur_cell.add_cover(delta, two_fx * delta);
         return;
     }
-    p     = (poly_base_size - fy1) * dx;
+    pdfium::base::CheckedNumeric<int> safeP = poly_base_size - fy1;
+    safeP *= dx;
+    if (!safeP.IsValid())
+      return;
     first = poly_base_size;
     if(dy < 0) {
-        p     = fy1 * dx;
-        first = 0;
-        incr  = -1;
-        dy    = -dy;
+      safeP = fy1;
+      safeP *= dx;
+      if (!safeP.IsValid())
+        return;
+      first = 0;
+      incr = -1;
+      dy = -dy;
     }
-    delta = p / dy;
-    mod   = p % dy;
+    delta = safeP.ValueOrDie() / dy;
+    mod = safeP.ValueOrDie() % dy;
     if(mod < 0) {
         delta--;
         mod += dy;
@@ -287,12 +294,15 @@ void outline_aa::render_line(int x1, int y1, int x2, int y2)
     ey1 += incr;
     set_cur_cell(x_from >> poly_base_shift, ey1);
     if(ey1 != ey2) {
-        p     = poly_base_size * dx;
-        lift  = p / dy;
-        rem   = p % dy;
-        if(rem < 0) {
-            lift--;
-            rem += dy;
+      safeP = static_cast<int>(poly_base_size);
+      safeP *= dx;
+      if (!safeP.IsValid())
+        return;
+      lift = safeP.ValueOrDie() / dy;
+      rem = safeP.ValueOrDie() % dy;
+      if (rem < 0) {
+        lift--;
+        rem += dy;
         }
         mod -= dy;
         while(ey1 != ey2) {