Commit 2516f31c for tesseract
commit 2516f31c560e056449b6d9e95e10c6feeb6ad618
Author: Stefan Weil <sw@weilnetz.de>
Date: Fri Nov 20 06:26:46 2026 +0100
classify: limit Min and Max instead of the range in ReadParamDesc
ReadParamDesc calculated the range in double and clipped it to the
range of float. That is not sufficient to avoid a floating point
overflow: with -O2 -fsanitize=address,undefined, clang 20 and newer
for arm64 convert the value to float after applying only the upper
limit and select the lower limit afterwards. For the limits 3e38 and
-3e38 that conversion sets FE_OVERFLOW, so
ParamDescTest.ToleratesHugeLimits failed in the scheduled macOS unit
tests (Apple clang 21).
Limit Min and Max to half of the float range when they are read, and
calculate Range, HalfRange and MidRange from them in float again. No
difference or sum can overflow then, whatever order the compiler
chooses. Limits beyond FLT_MAX / 2 are now stored clipped; before,
only Range was clipped.
Assisted-by: Claude Fable 5.2 (Anthropic)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/src/classify/clusttool.cpp b/src/classify/clusttool.cpp
index d14d9971..938c904c 100644
--- a/src/classify/clusttool.cpp
+++ b/src/classify/clusttool.cpp
@@ -151,14 +151,18 @@ PARAM_DESC *ReadParamDesc(TFile *fp, uint16_t N) {
ASSERT_HOST(!stream.fail());
ParamDesc[i].Circular = (linear_token[0] == 'c');
ParamDesc[i].NonEssential = (essential_token[0] != 'e');
- // Min and Max come from the file and can be any float value, so compute
- // in double and clip the range to avoid a floating point overflow
- // (which the tesseract program traps with SIGFPE).
- const double min = ParamDesc[i].Min;
- const double max = ParamDesc[i].Max;
- ParamDesc[i].Range = std::clamp<double>(max - min, -FLT_MAX, FLT_MAX);
+ // Min and Max come from the file and can be any float value. Limit them
+ // to half of the float range, so neither their difference nor their sum
+ // can overflow (the tesseract program traps a floating point overflow
+ // with SIGFPE). Clipping the calculated range is not sufficient, as a
+ // compiler may convert the unclipped value to float before it selects
+ // the limit (clang for arm64), which sets the overflow flag.
+ const float kMaxLimit = FLT_MAX / 2;
+ ParamDesc[i].Min = std::clamp(ParamDesc[i].Min, -kMaxLimit, kMaxLimit);
+ ParamDesc[i].Max = std::clamp(ParamDesc[i].Max, -kMaxLimit, kMaxLimit);
+ ParamDesc[i].Range = ParamDesc[i].Max - ParamDesc[i].Min;
ParamDesc[i].HalfRange = ParamDesc[i].Range / 2;
- ParamDesc[i].MidRange = (max + min) / 2;
+ ParamDesc[i].MidRange = (ParamDesc[i].Max + ParamDesc[i].Min) / 2;
}
return (ParamDesc);
}