Commit da95ef0a for tesseract
commit da95ef0a5d61e1a76264a9fad01e702c0c2347bc
Author: Stefan Weil <sw@weilnetz.de>
Date: Fri Nov 13 22:31:46 2026 +0100
classify: do not use the integer matcher with too many features
The integer matcher and INT_FX_RESULT_STRUCT count the features of a
blob in an int16_t. A blob with more than 32767 features got a wrong
or negative count. With a count of 65536 - n, where n is the length
of a config, NormalizeSums() divided by zero (SIGFPE).
Check the number of features in the callers of the matcher and in
BlobToTrainingSample(), and handle such a blob like one without
features.
Fix proposed by the Anthropic OSS Scanner (finding ANT-2026-WXIBTAOL).
Assisted-by: OpenCode / Claude Fable 5.2 (Anthropic)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/src/classify/adaptmatch.cpp b/src/classify/adaptmatch.cpp
index 7ec9ab4a..15945139 100644
--- a/src/classify/adaptmatch.cpp
+++ b/src/classify/adaptmatch.cpp
@@ -66,7 +66,7 @@
#include <algorithm> // for max, min
#include <cassert> // for assert
#include <cmath> // for fabs
-#include <cstdint> // for INT32_MAX, UINT8_MAX
+#include <cstdint> // for INT16_MAX, INT32_MAX, UINT8_MAX
#include <cstdio> // for fflush, fclose, fopen, stdout, FILE
#include <cstring> // for strstr, memset, strcmp
#include <unordered_map> // for unordered_map
@@ -1003,7 +1003,9 @@ void Classify::DisplayAdaptedChar(TBLOB *blob, INT_CLASS_STRUCT *int_class) {
std::vector<INT_FEATURE_STRUCT> bl_features;
TrainingSample *sample =
BlobToTrainingSample(*blob, classify_nonlinear_norm, &fx_info, &bl_features);
- if (sample == nullptr) {
+ // The integer matcher counts features in an int16_t.
+ if (sample == nullptr || bl_features.size() > INT16_MAX) {
+ delete sample;
return;
}
@@ -1100,7 +1102,8 @@ void Classify::AmbigClassifier(const std::vector<INT_FEATURE_STRUCT> &int_featur
const INT_FX_RESULT_STRUCT &fx_info, const TBLOB *blob,
INT_TEMPLATES_STRUCT *templates,
UNICHAR_ID *ambiguities, ADAPT_RESULTS *results) {
- if (int_features.empty()) {
+ // The integer matcher counts features in an int16_t.
+ if (int_features.empty() || int_features.size() > INT16_MAX) {
return;
}
auto *CharNormArray = new uint8_t[unicharset.size()];
@@ -1290,7 +1293,8 @@ UNICHAR_ID *Classify::BaselineClassifier(TBLOB *Blob,
const std::vector<INT_FEATURE_STRUCT> &int_features,
const INT_FX_RESULT_STRUCT &fx_info,
ADAPT_TEMPLATES_STRUCT *Templates, ADAPT_RESULTS *Results) {
- if (int_features.empty()) {
+ // The integer matcher counts features in an int16_t.
+ if (int_features.empty() || int_features.size() > INT16_MAX) {
return nullptr;
}
auto *CharNormArray = new uint8_t[unicharset.size()];
@@ -1357,6 +1361,10 @@ int Classify::CharNormTrainingSample(bool pruner_only, int keep_this, const Trai
adapt_results->Initialize();
// Compute the bounding box of the features.
uint32_t num_features = sample.num_features();
+ // The integer matcher counts features in an int16_t.
+ if (num_features > INT16_MAX) {
+ return num_features;
+ }
// Only the top and bottom of the blob_box are used by MasterMatcher, so
// fabricate right and left using top and bottom.
TBOX blob_box(sample.geo_feature(GeoBottom), sample.geo_feature(GeoBottom),
diff --git a/src/classify/intfx.cpp b/src/classify/intfx.cpp
index f463297e..711f91c7 100644
--- a/src/classify/intfx.cpp
+++ b/src/classify/intfx.cpp
@@ -31,8 +31,9 @@
#include "helpers.h"
-#include <cmath> // for M_PI
-#include <mutex> // for std::mutex
+#include <cmath> // for M_PI
+#include <cstdint> // for INT16_MAX
+#include <mutex> // for std::mutex
namespace tesseract {
@@ -78,9 +79,9 @@ TrainingSample *BlobToTrainingSample(const TBLOB &blob, bool nonlinear_norm,
// TODO(rays) Use blob->PreciseBoundingBox() instead.
TBOX box = blob.bounding_box();
TrainingSample *sample = nullptr;
- int num_features = fx_info->NumCN;
- if (num_features > 0) {
- sample = TrainingSample::CopyFromFeatures(*fx_info, box, &cn_features[0], num_features);
+ // fx_info->NumCN and the integer matcher count features in an int16_t.
+ if (!cn_features.empty() && cn_features.size() <= INT16_MAX) {
+ sample = TrainingSample::CopyFromFeatures(*fx_info, box, &cn_features[0], cn_features.size());
}
if (sample != nullptr) {
// Set the bounding box (in original image coordinates) in the sample.