Fix infinite loop in SkRTree::insert()
authorMike Klein <mtklein@chromium.org>
Fri, 7 Apr 2017 14:46:39 +0000 (10:46 -0400)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Mon, 10 Apr 2017 13:11:34 +0000 (13:11 +0000)
When aspectRatio is not finite, insert() can fall into an infinite loop.
This happens if you pass SkRect::MakeLargest() to the factory as bounds.

BUG=skia:5974

Change-Id: Ibcc9e5c5943c718608d4c1448305f7b8f11413bc
Reviewed-on: https://skia-review.googlesource.com/11784
Reviewed-by: Mike Klein <mtklein@chromium.org>
Commit-Queue: Mike Klein <mtklein@chromium.org>

src/core/SkRTree.cpp
tests/PictureBBHTest.cpp

index bae2fdc..4d1787e 100644 (file)
@@ -7,7 +7,8 @@
 
 #include "SkRTree.h"
 
-SkRTree::SkRTree(SkScalar aspectRatio) : fCount(0), fAspectRatio(aspectRatio) {}
+SkRTree::SkRTree(SkScalar aspectRatio)
+    : fCount(0), fAspectRatio(isfinite(aspectRatio) ? aspectRatio : 1) {}
 
 SkRect SkRTree::getRootBound() const {
     if (fCount) {
index 69cb443..64b09b1 100644 (file)
@@ -91,3 +91,15 @@ DEF_TEST(PictureBBH, reporter) {
     EmptyClipPictureBBHTest emptyClipPictureTest;
     emptyClipPictureTest.run(reporter);
 }
+
+DEF_TEST(RTreeMakeLargest, r) {
+    // A call to insert() with 2 or more rects and a bounds of SkRect::MakeLargest()
+    // used to fall into an infinite loop.
+
+    SkRTreeFactory factory;
+    std::unique_ptr<SkBBoxHierarchy> bbh{ factory(SkRect::MakeLargest()) };
+
+    SkRect rects[] = { {0,0, 10,10}, {5,5,15,15} };
+    bbh->insert(rects, SK_ARRAY_COUNT(rects));
+    REPORTER_ASSERT(r, bbh->getRootBound() == SkRect::MakeWH(15,15));
+}