Commit 3851f417 for tesseract
commit 3851f4178551fc9ea86768900190ef31255cbbdb
Author: Stefan Weil <sw@weilnetz.de>
Date: Fri Nov 13 21:26:30 2026 +0100
textord: avoid an overflow of the weights in ComputeLimits
ColPartition::ComputeLimits() adds the area of the bounding box of
each blob as the weight to six STATS histograms, which count with
int32_t. The boxes of different blobs can overlap, so the areas of
the blobs of one partition can add up to more than 2^31. The counts
wrapped then, and the next STATS::median() failed an assertion in
STATS::ile(), which aborted the process.
Divide the areas by a common divisor if their sum does not fit. The
divisor is 1, and nothing changes, for partitions which were handled
correctly before.
Fix proposed by the Anthropic OSS Scanner (finding ANT-2026-Y1QPI9PY).
Assisted-by: OpenCode / Claude Fable 5.2 (Anthropic)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/src/textord/colpartition.cpp b/src/textord/colpartition.cpp
index 509ff096..b8a35b37 100644
--- a/src/textord/colpartition.cpp
+++ b/src/textord/colpartition.cpp
@@ -955,11 +955,18 @@ void ColPartition::ComputeLimits() {
STATS left_stats(bounding_box_.left(), bounding_box_.right());
STATS right_stats(bounding_box_.left(), bounding_box_.right());
STATS width_stats(0, bounding_box_.width());
+ // STATS keeps int32_t counts, but the areas of the (possibly overlapping)
+ // blob boxes can sum to more than that, so scale the weights down to fit.
+ int64_t total_area = 0;
+ for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
+ total_area += it.data()->bounding_box().area();
+ }
+ int area_divisor = static_cast<int>(total_area / INT32_MAX) + 1;
for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
bbox = it.data();
if (non_leader_count == 0 || bbox->flow() != BTFT_LEADER) {
const TBOX &box = bbox->bounding_box();
- int area = box.area();
+ int area = box.area() / area_divisor;
top_stats.add(box.top(), area);
bottom_stats.add(box.bottom(), area);
height_stats.add(box.height(), area);