Commit 023f16ea for tesseract

commit 023f16ea031859d4f716e61edffed79d3ad857b5
Author: Stefan Weil <sw@weilnetz.de>
Date:   Fri Nov 13 21:26:30 2026 +0100

    textord: avoid an overflow of the totals in TabVector::FitVector

    FitVector() adds the length of each line which is not ragged,
    multiplied by its number of segments, to two 32 bit totals. The
    callers scale the totals to a 16 bit ICOORD and expect that they are
    not negative. A page with many long dashed rules wrapped the totals,
    the vertical direction became (x, 0), and
    AlignedBlob::FindAlignedBlob() divided by its y coordinate (SIGFPE).
    Another kind of page ended with layout grids without any cell.

    Calculate the new totals with 64 bits and keep the old ones if the
    new ones no longer fit. The totals only give a direction, so that
    does not change the estimate noticeably.

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

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

diff --git a/src/textord/tabvector.cpp b/src/textord/tabvector.cpp
index 604afa6a..4f099b50 100644
--- a/src/textord/tabvector.cpp
+++ b/src/textord/tabvector.cpp
@@ -29,6 +29,8 @@
 #include "tabvector.h"

 #include <algorithm>
+#include <cstdint> // for int64_t, INT32_MAX
+#include <cstdlib> // for std::abs

 namespace tesseract {

@@ -184,8 +186,15 @@ TabVector *TabVector::FitVector(TabAlignment alignment, ICOORD vertical, int ext
   if (!vector->IsRagged()) {
     vertical = vector->endpt_ - vector->startpt_;
     int weight = vector->BoxCount();
-    *vertical_x += vertical.x() * weight;
-    *vertical_y += vertical.y() * weight;
+    // The totals only encode a direction, but a page with many long lines
+    // made of many pieces can overflow them, so stop adding once they are
+    // full. Otherwise a wrapped total could be scaled to a zero vertical.
+    int64_t total_x = *vertical_x + static_cast<int64_t>(vertical.x()) * weight;
+    int64_t total_y = *vertical_y + static_cast<int64_t>(vertical.y()) * weight;
+    if (std::abs(total_x) <= INT32_MAX && std::abs(total_y) <= INT32_MAX) {
+      *vertical_x = static_cast<int>(total_x);
+      *vertical_y = static_cast<int>(total_y);
+    }
   }
   return vector;
 }