Commit eb309eef for tesseract

commit eb309eef66205188a842e30ef9f20e53e70ad92b
Author: Stefan Weil <sw@weilnetz.de>
Date:   Wed Dec 2 22:15:19 2026 +0100

    Fix signed integer overflow in fix2

    The first step of the polygonal approximation of an outline removes
    fixed points which are close to each other. The limit for the squared
    distance is area * 400 / 44000, where area is the square of the height
    of the outline (or of its width), and the product was calculated with
    32 bit integers. It overflows for outlines which are larger than 2317
    pixels:

        src/ccstruct/blobs.cpp:381:30: runtime error: signed integer
        overflow: 5760000 * 400 cannot be represented in type 'int'

    Calculate the product with 64 bits. The result still fits in an int.

    Nothing changes for smaller outlines. For larger ones the result
    depended on the build: g++ 13 with -O2, ASan and UBSan got a negative
    limit for an outline of 2400 pixels and removed no point (9 instead
    of 4 points for the outline of the new unit test), while a release
    build with the same compiler already gave 4 points.

    Add a unit test.

    Assisted-by: Claude Fable 5.2 (Anthropic)
    Signed-off-by: Stefan Weil <sw@weilnetz.de>

diff --git a/src/ccstruct/blobs.cpp b/src/ccstruct/blobs.cpp
index eff11404..174fff0f 100644
--- a/src/ccstruct/blobs.cpp
+++ b/src/ccstruct/blobs.cpp
@@ -383,7 +383,9 @@ static void fix2(  // polygonal approx
     area = 450;
   }

-  gapmin = area * fixed_dist * fixed_dist / 44000;
+  // The product needs 64 bits for outlines which are larger than 2317 pixels.
+  gapmin = static_cast<int>(static_cast<int64_t>(area) * fixed_dist *
+                            fixed_dist / 44000);

   edgept = start;
   fixed_count = 0;
diff --git a/unittest/blobs_test.cc b/unittest/blobs_test.cc
index 1e119311..be80065f 100644
--- a/unittest/blobs_test.cc
+++ b/unittest/blobs_test.cc
@@ -28,6 +28,12 @@
 #include "include_gunit.h"

 #include "blobs.h"
+#include "coutln.h"   // for C_OUTLINE
+#include "mod128.h"   // for DIR128
+#include "stepblob.h" // for C_BLOB
+
+#include <memory> // for std::unique_ptr
+#include <vector> // for std::vector

 namespace tesseract {

@@ -121,6 +127,42 @@ TEST(EliminateDuplicateOutlinesTest, SameBoxDifferentCycleIsKept) {
   EXPECT_EQ(CycleSize(outline2->loop), 4);
 }

+// Returns the number of points of the polygonal approximation of an outline
+// which is a rectangle with a square bump in the middle of its right side.
+int PolygonSizeOfRectangleWithBump(int width, int height, int bump) {
+  // Directions of the steps of an outline.
+  const int kWest = 0, kSouth = 32, kEast = 64, kNorth = 96;
+  std::vector<DIR128> steps;
+  auto add_steps = [&steps](int dir, int count) {
+    steps.insert(steps.end(), count, DIR128(static_cast<int16_t>(dir)));
+  };
+  const int below = (height - bump) / 2;
+  add_steps(kEast, width);
+  add_steps(kNorth, below);
+  add_steps(kEast, bump);
+  add_steps(kNorth, bump);
+  add_steps(kWest, bump);
+  add_steps(kNorth, height - bump - below);
+  add_steps(kWest, width);
+  add_steps(kSouth, height);
+  // The blob takes the ownership of the outline.
+  C_BLOB blob(new C_OUTLINE(ICOORD(0, 0), &steps[0],
+                            static_cast<int16_t>(steps.size())));
+  std::unique_ptr<TBLOB> tblob(TBLOB::PolygonalCopy(false, &blob));
+  return CycleSize(tblob->outlines->loop);
+}
+
+// The polygonal approximation removes points which are close to each other.
+// The limit for that grows with the size of the outline, and its calculation
+// had a signed integer overflow for outlines which are larger than 2317
+// pixels. The limit was negative then, so no point was removed.
+TEST(PolygonalCopyTest, LargeOutlineLosesSmallBump) {
+  // A bump of 4 pixels is too small to be kept for outlines of this size.
+  EXPECT_EQ(PolygonSizeOfRectangleWithBump(10, 2000, 4), 4);
+  // 2400 * 2400 * 400 is more than INT32_MAX.
+  EXPECT_EQ(PolygonSizeOfRectangleWithBump(10, 2400, 4), 4);
+}
+
 } // namespace

 } // namespace tesseract