xref: /aosp_15_r20/external/pdfium/third_party/agg23/0014-ubsan-render-line.patch (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1diff --git a/third_party/agg23/agg_rasterizer_scanline_aa.cpp b/third_party/agg23/agg_rasterizer_scanline_aa.cpp
2index d2b6a46e4..2f19a1816 100644
3--- a/third_party/agg23/agg_rasterizer_scanline_aa.cpp
4+++ b/third_party/agg23/agg_rasterizer_scanline_aa.cpp
5@@ -227,10 +227,27 @@ AGG_INLINE void outline_aa::render_hline(int ey, int x1, int y1, int x2, int y2)
6 void outline_aa::render_line(int x1, int y1, int x2, int y2)
7 {
8     enum dx_limit_e { dx_limit = 16384 << poly_base_shift };
9-    int dx = x2 - x1;
10+    pdfium::base::CheckedNumeric<int> safe_dx = x2;
11+    safe_dx -= x1;
12+    if (!safe_dx.IsValid())
13+        return;
14+
15+    int dx = safe_dx.ValueOrDie();
16     if(dx >= dx_limit || dx <= -dx_limit) {
17-        int cx = (x1 + x2) >> 1;
18-        int cy = (y1 + y2) >> 1;
19+        pdfium::base::CheckedNumeric<int> safe_cx = x1;
20+        safe_cx += x2;
21+        safe_cx /= 2;
22+        if (!safe_cx.IsValid())
23+            return;
24+
25+        pdfium::base::CheckedNumeric<int> safe_cy = y1;
26+        safe_cy += y2;
27+        safe_cy /= 2;
28+        if (!safe_cy.IsValid())
29+            return;
30+
31+        int cx = safe_cx.ValueOrDie();
32+        int cy = safe_cy.ValueOrDie();
33         render_line(x1, y1, cx, cy);
34         render_line(cx, cy, x2, y2);
35     }
36