Commit 1350d38e for tesseract
commit 1350d38eb2845a01b71371891444b20b0926bfc9
Author: Stefan Weil <sw@weilnetz.de>
Date: Tue Dec 1 17:02:17 2026 +0100
Fix signed integer overflow in cutline
The polygonal approximation of an outline squares the deviation of
each point from a straight line and adds up the squares, all with 32
bit integers. A deviation is the cross product of two vectors in the
outline, so it grows with the area of the outline, and the squares
overflow for outlines which are larger than normal characters:
src/ccstruct/blobs.cpp:126:15: runtime error: signed integer
overflow: 1947416598 + 266538276 cannot be represented in type 'int'
OSS-Fuzz found that with fuzzer-raw-image and an image of 42 x 807
pixels (issue 591174190).
Use 64 bit integers for the squared deviations, for their sum and for
the limit which they are compared with. Nothing changes for values
which did not overflow.
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 69a28228..eff11404 100644
--- a/src/ccstruct/blobs.cpp
+++ b/src/ccstruct/blobs.cpp
@@ -45,7 +45,8 @@
#include "helpers.h"
#include <algorithm>
-#include <cstdint> // for INT16_MAX, int8_t
+#include <cinttypes> // for PRId64
+#include <cstdint> // for INT16_MAX, int64_t, int8_t
namespace tesseract {
@@ -86,10 +87,13 @@ static void cutline( // recursive refine
int vlen; // approx length of vecsum
TPOINT vec; // accumulated vector
EDGEPT *maxpoint; // worst point
- int maxperp; // max deviation
- int perp; // perp distance
- int ptcount; // no of points
- int squaresum; // sum of perps
+ // The squared deviations and their sum need 64 bits. A deviation is the
+ // cross product of two vectors in the outline, so it can be as large as
+ // twice the area of the bounding box of the outline.
+ int64_t maxperp; // max deviation
+ int64_t perp; // perp distance
+ int ptcount; // no of points
+ int64_t squaresum; // sum of perps
edge = first; // start of line
if (edge->next == last) {
@@ -126,7 +130,7 @@ static void cutline( // recursive refine
squaresum += perp; // sum squares
ptcount++; // count points
if (poly_debug) {
- tprintf("Cutline:Final perp=%d\n", perp);
+ tprintf("Cutline:Final perp=%" PRId64 "\n", perp);
}
if (perp > maxperp) {
maxperp = perp;
@@ -160,7 +164,8 @@ static void cutline( // recursive refine
maxperp / 256.0, maxperp * 200.0 / area, perp / 256.0,
perp * 300.0 / area);
}
- if (maxperp * par1 >= 10 * area || perp * par2 >= 10 * area || vlen >= 126) {
+ const int64_t limit = 10 * static_cast<int64_t>(area);
+ if (maxperp * par1 >= limit || perp * par2 >= limit || vlen >= 126) {
maxpoint->fixed = true;
// partitions
cutline(first, maxpoint, area);