detect if the computed rgn bounds is too big

Bug:801869
Change-Id: I7380bfb86aedc719cf67e20e918ef39d1b143aee
Reviewed-on: https://skia-review.googlesource.com/95020
Commit-Queue: Mike Reed <reed@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
diff --git a/src/core/SkRegion.cpp b/src/core/SkRegion.cpp
index 8c402e9..3a1a63e 100644
--- a/src/core/SkRegion.cpp
+++ b/src/core/SkRegion.cpp
@@ -289,6 +289,11 @@
     memcpy(fRunHead->writable_runs(), runs, count * sizeof(RunType));
     fRunHead->computeRunBounds(&fBounds);
 
+    // Our computed bounds might be too large, so we have to check here.
+    if (fBounds.isEmpty()) {
+        return this->setEmpty();
+    }
+
     SkDEBUGCODE(this->validate();)
 
     return true;
diff --git a/tests/RegionTest.cpp b/tests/RegionTest.cpp
index 7fcf101..be84d14 100644
--- a/tests/RegionTest.cpp
+++ b/tests/RegionTest.cpp
@@ -396,3 +396,26 @@
         REPORTER_ASSERT(r, 0 == region.readFromMemory(data, sizeof(data)));
     }
 }
+
+DEF_TEST(region_toobig, reporter) {
+    const int big = 1 << 30;
+    const SkIRect neg = SkIRect::MakeXYWH(-big, -big, 10, 10);
+    const SkIRect pos = SkIRect::MakeXYWH( big,  big, 10, 10);
+
+    REPORTER_ASSERT(reporter, !neg.isEmpty());
+    REPORTER_ASSERT(reporter, !pos.isEmpty());
+
+    SkRegion negR(neg);
+    SkRegion posR(pos);
+
+    REPORTER_ASSERT(reporter, !negR.isEmpty());
+    REPORTER_ASSERT(reporter, !posR.isEmpty());
+
+    SkRegion rgn;
+    rgn.op(negR, posR, SkRegion::kUnion_Op);
+
+    // If we union those to rectangles, the resulting coordinates span more than int32_t, so
+    // we must mark the region as empty.
+    REPORTER_ASSERT(reporter, rgn.isEmpty());
+}
+