Commit 88f99fe5 for tesseract

commit 88f99fe5b495b6c5f43406eb809825ee8011bae2
Author: Stefan Weil <sw@weilnetz.de>
Date:   Fri Nov 13 21:26:18 2026 +0100

    ccstruct: clip the result of ICOORD::rotate

    Rotating a point near the far corner of a page with a side of nearly
    32767 pixels can give a coordinate outside of the range of TDimension
    (int16_t). The conversion wrapped such values, so the code which
    deskews the blobs of a skewed page got boxes far outside of the page.
    The results were writes behind arrays in
    TableFinder::GetTableRegions(), a failed assertion in C_OUTLINE and
    an endless loop in test_underline().

    Clip the rotated coordinates to the range of TDimension.
    POLY_BLOCK::rotate() now uses ICOORD::rotate(), so it gets the same
    clipping. Also clip the baseline which is passed to test_underline(),
    and let the loop counter of that function exceed the range of
    TDimension.

    Fix proposed by the Anthropic OSS Scanner (finding ANT-2026-Q33BX03Y).

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

diff --git a/src/ccstruct/points.h b/src/ccstruct/points.h
index 268bb670..e597cecc 100644
--- a/src/ccstruct/points.h
+++ b/src/ccstruct/points.h
@@ -21,12 +21,14 @@

 #include "elst.h"
 #include "errcode.h" // for ASSERT_HOST
+#include "helpers.h" // for ClipToRange
 #include "tesstypes.h" // for TDimension

 #include <tesseract/export.h> // for DLLSYM

 #include <cmath> // for sqrt, atan2
 #include <cstdio>
+#include <limits> // for std::numeric_limits

 namespace tesseract {

@@ -499,8 +501,13 @@ constexpr inline ICOORD &operator/=( // scalar divide

 inline void ICOORD::rotate( // rotate by vector
     const FCOORD &vec) {
-  auto tmp = static_cast<TDimension>(std::floor(xcoord * vec.x() - ycoord * vec.y() + 0.5f));
-  ycoord = static_cast<TDimension>(std::floor(ycoord * vec.x() + xcoord * vec.y() + 0.5f));
+  // A point near the far corner of a large image can rotate out of the
+  // range of TDimension, so clip the result to keep the conversion defined.
+  constexpr double limit = std::numeric_limits<TDimension>::max();
+  auto tmp = static_cast<TDimension>(
+      ClipToRange<double>(std::floor(xcoord * vec.x() - ycoord * vec.y() + 0.5f), -limit, limit));
+  ycoord = static_cast<TDimension>(
+      ClipToRange<double>(std::floor(ycoord * vec.x() + xcoord * vec.y() + 0.5f), -limit, limit));
   xcoord = tmp;
 }

diff --git a/src/ccstruct/polyblk.cpp b/src/ccstruct/polyblk.cpp
index 993e10b6..5edb27cb 100644
--- a/src/ccstruct/polyblk.cpp
+++ b/src/ccstruct/polyblk.cpp
@@ -187,17 +187,11 @@ bool POLY_BLOCK::contains(POLY_BLOCK *other) {
  */

 void POLY_BLOCK::rotate(FCOORD rotation) {
-  FCOORD pos;                   // current pos;
-  ICOORDELT *pt;                // current point
   ICOORDELT_IT pts = &vertices; // iterator

   do {
-    pt = pts.data();
-    pos.set_x(pt->x());
-    pos.set_y(pt->y());
-    pos.rotate(rotation);
-    pt->set_x(static_cast<TDimension>(floor(pos.x() + 0.5)));
-    pt->set_y(static_cast<TDimension>(floor(pos.y() + 0.5)));
+    // ICOORD::rotate rounds and clips the result to the range of TDimension.
+    pts.data()->rotate(rotation);
     pts.forward();
   } while (!pts.at_first());
   compute_bb();
diff --git a/src/textord/blkocc.cpp b/src/textord/blkocc.cpp
index 60283e74..81f9c5cf 100644
--- a/src/textord/blkocc.cpp
+++ b/src/textord/blkocc.cpp
@@ -50,7 +50,7 @@ bool test_underline(  // look for underlines
     int16_t baseline, ///< coords of baseline
     int16_t xheight   ///< height of line
 ) {
-  TDimension occ;
+  int32_t occ; // must be able to go past the range of TDimension
   STATS projection;

   auto blob_box = blob->bounding_box();
diff --git a/src/textord/makerow.cpp b/src/textord/makerow.cpp
index 36ec0a1f..d506b281 100644
--- a/src/textord/makerow.cpp
+++ b/src/textord/makerow.cpp
@@ -28,6 +28,7 @@
 #include "ccstruct.h"
 #include "detlinefit.h"
 #include "drawtord.h"
+#include "helpers.h" // for ClipToRange
 #include "oldbasel.h"
 #include "sortflts.h"
 #include "statistc.h"
@@ -1823,7 +1824,8 @@ void separate_underlines(TO_BLOCK *block,   // block to do
         ASSERT_HOST(blob->cblob() != nullptr);
         rotated_blob = crotate_cblob(blob->cblob(), blob_rotation);
         if (test_underline(testing_on && textord_show_final_rows, rotated_blob,
-                           static_cast<int16_t>(row->intercept()),
+                           static_cast<int16_t>(
+                               ClipToRange<float>(row->intercept(), -INT16_MAX, INT16_MAX)),
                            static_cast<int16_t>(block->line_size *
                                                 (tesseract::CCStruct::kXHeightFraction +
                                                  tesseract::CCStruct::kAscenderFraction / 2.0f)))) {