Fix assert in SkBlurEngine/SkBitmap

This was a curious one to reproduce because it only reproduced
under the following GN args:
    extra_cflags = [ "-DSK_AVOID_SLOW_RASTER_PIPELINE_BLURS=1" ]
    is_debug = true

I could not get an ASAN release to fire, despite the overflow in
SkBitmap::getAddr causing an assert and a bad pointer to be
calculated, because the bad pointer is not used.

The test case gets into eval_blur_passes with:
srcBounds = {fLeft = 0, fTop = 0, fRight = 1, fBottom = 64}
dstBounds = {fLeft = 1, fTop = 99, fRight = 4, fBottom = 165}
and a makerX->window() of exactly 1 so we skip the x only blur.
loopStart and loopEnd both become 1, but in the old code, we
computed the srcAddr and dstAddr w/o checking that the loop start
is actually in the range of the src bounds, which it isn't.
This caused an assert because the address calculated is off
the right side, but the Y loop wouldn't run because loopStart
is not less than loopEnd.

Now by checking the loopStart and loopEnd, we'll prevent
even calculating the offset. Because loopEnd <= srcBounds.right()
we'll avoid walking off the end.

Change-Id: Id1cddf0cbc4360cba9f18bcae9f088c73610fcaf
Bug: 508075339
Fixed: 508075339
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1225736
Auto-Submit: Kaylee Lubick <kjlubick@google.com>
Commit-Queue: Kaylee Lubick <kjlubick@google.com>
Reviewed-by: Michael Ludwig <michaelludwig@google.com>
diff --git a/src/core/SkBlurEngine.cpp b/src/core/SkBlurEngine.cpp
index 12f63b8..d7b5c3e 100644
--- a/src/core/SkBlurEngine.cpp
+++ b/src/core/SkBlurEngine.cpp
@@ -203,19 +203,21 @@
         loopStart = std::max(srcBounds.top(),    dstBounds.top());
         loopEnd   = std::min(srcBounds.bottom(), dstBounds.bottom());
 
-        auto srcAddr = reinterpret_cast<T*>(src.getAddr(0, loopStart - srcBounds.top()));
-        auto dstAddr = reinterpret_cast<T*>(dst.getAddr(0, loopStart - dstBounds.top()));
+        if (loopStart < loopEnd) {
+            auto srcAddr = reinterpret_cast<T*>(src.getAddr(0, loopStart - srcBounds.top()));
+            auto dstAddr = reinterpret_cast<T*>(dst.getAddr(0, loopStart - dstBounds.top()));
 
-        // Iterate over each row to calculate 1D blur along X.
-        Pass* pass = makerX->makePass(buffer, alloc);
-        for (int y = loopStart; y < loopEnd; ++y) {
-            pass->blur<T>(srcBounds.left()  - dstBounds.left(),
-                          srcBounds.right() - dstBounds.left(),
-                          dstBounds.width(),
-                          srcAddr, 1,
-                          dstAddr, 1);
-            srcAddr += src.rowBytesAsPixels();
-            dstAddr += dst.rowBytesAsPixels();
+            // Iterate over each row to calculate 1D blur along X.
+            Pass* pass = makerX->makePass(buffer, alloc);
+            for (int y = loopStart; y < loopEnd; ++y) {
+                pass->blur<T>(srcBounds.left()  - dstBounds.left(),
+                              srcBounds.right() - dstBounds.left(),
+                              dstBounds.width(),
+                              srcAddr, 1,
+                              dstAddr, 1);
+                srcAddr += src.rowBytesAsPixels();
+                dstAddr += dst.rowBytesAsPixels();
+            }
         }
 
         // Set up the Y pass to blur from the full dst into the non-outset portion of dst
@@ -235,18 +237,20 @@
     // into dst for a 1D blur; or it's blurring from dst into dst for the second pass of a 2D
     // blur.
     if (makerY->window() > 1) {
-        auto srcAddr = reinterpret_cast<T*>(src.getAddr(loopStart - srcBounds.left(), 0));
-        auto dstAddr = reinterpret_cast<T*>(dst.getAddr(loopStart - dstBounds.left(), dstYOffset));
+        if (loopStart < loopEnd) {
+            auto srcAddr = reinterpret_cast<T*>(src.getAddr(loopStart - srcBounds.left(), 0));
+            auto dstAddr = reinterpret_cast<T*>(dst.getAddr(loopStart - dstBounds.left(), dstYOffset));
 
-        Pass* pass = makerY->makePass(buffer, alloc);
-        for (int x = loopStart; x < loopEnd; ++x) {
-            pass->blur<T>(srcBounds.top()    - dstBounds.top(),
-                          srcBounds.bottom() - dstBounds.top(),
-                          dstBounds.height(),
-                          srcAddr, src.rowBytesAsPixels(),
-                          dstAddr, dst.rowBytesAsPixels());
-            srcAddr += 1;
-            dstAddr += 1;
+            Pass* pass = makerY->makePass(buffer, alloc);
+            for (int x = loopStart; x < loopEnd; ++x) {
+                pass->blur<T>(srcBounds.top()    - dstBounds.top(),
+                              srcBounds.bottom() - dstBounds.top(),
+                              dstBounds.height(),
+                              srcAddr, src.rowBytesAsPixels(),
+                              dstAddr, dst.rowBytesAsPixels());
+                srcAddr += 1;
+                dstAddr += 1;
+            }
         }
     }
 
diff --git a/tests/BlurTest.cpp b/tests/BlurTest.cpp
index b601c7b..99280dc 100644
--- a/tests/BlurTest.cpp
+++ b/tests/BlurTest.cpp
@@ -25,6 +25,7 @@
 #include "include/core/SkSize.h"
 #include "include/core/SkSurface.h"
 #include "include/core/SkTypes.h"
+#include "include/effects/SkImageFilters.h"
 #include "include/effects/SkPerlinNoiseShader.h"
 #include "include/gpu/GpuTypes.h"
 #include "include/private/base/SkTPin.h"
@@ -666,3 +667,41 @@
 }
 
 #endif // SK_GRAPHITE
+
+DEF_TEST(blur_b508075339, reporter) {
+    // This test verifies that eval_blur_passes correctly handles a specific edge case where
+    // the source and destination bounds do not intersect on the axis being processed.
+    //
+    // The parameters are chosen to create the following scenario:
+    // 1. A very small sigmaX (0.390625f) evaluates to a 1D blur window of 1 on the X-axis
+    //    (when SK_AVOID_SLOW_RASTER_PIPELINE_BLURS is active). A window of 1 causes the
+    //    blur engine to skip the X-pass entirely.
+    // 2. A large negative offsetX (-65.618912f) shifts the source image completely off the
+    //    left side of the 64x64 canvas.
+    // 3. Despite being off-canvas, the blur radius expands the transparent pixels just enough
+    //    that they touch the canvas boundary. This triggers padding logic (makePixelOutset)
+    //    that slightly expands the destination bounds relative to the source.
+    //
+    // Historically, skipping the X-pass combined with this artificial bounds padding meant
+    // that the starting X-coordinate for the Y-pass (loopStart) could be greater than the
+    // source image width.
+    constexpr int kCanvasSize = 64;
+    constexpr float sigmaX = 0.390625f;
+    constexpr float sigmaY = 99.609375f;
+    constexpr float offsetX = -65.618912f;
+    constexpr float offsetY = -99.999985f;
+
+    auto inner = SkImageFilters::Offset(offsetX, offsetY, nullptr);
+    auto filter = SkImageFilters::Blur(sigmaX, sigmaY, std::move(inner));
+
+    auto surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(kCanvasSize, kCanvasSize));
+    REPORTER_ASSERT(reporter, surface);
+    SkCanvas* canvas = surface->getCanvas();
+
+    SkPaint paint;
+    paint.setImageFilter(std::move(filter));
+
+    // This drawRect triggers the image filter graph evaluation. The test passes if it
+    // completes without triggering an SkASSERT in SkBitmap::getAddr.
+    canvas->drawRect(SkRect::MakeWH(kCanvasSize, kCanvasSize), paint);
+}