Commit de9252bb for tesseract
commit de9252bb59a803c7f01d7a4ffa5b99235bfe17dd
Author: Stefan Weil <sw@weilnetz.de>
Date: Tue Dec 1 17:02:17 2026 +0100
Fix heap buffer overflow in the QSPLINE constructor
The constructor adds an interpolated point at the start of each
segment of the spline. For a segment which starts after the last
point it took the element after the last point for that, which is
beyond the end of the arrays if they have no more elements:
ERROR: AddressSanitizer: heap-buffer-overflow
READ of size 4
#0 tesseract::QSPLINE::QSPLINE(int*, int, int*, int*, int, int)
src/ccstruct/quspline.cpp:106:27
#1 tesseract::Textord::find_textlines(...)
src/textord/oldbasel.cpp:385:25
OSS-Fuzz found that with fuzzer-raw-image (issue 591595917). The last
two points of a row were at x = -22, and segment_spline made a last
segment from -21 to -21 for them, because it rounds the mean of
negative x coordinates up.
Test whether there is such a point, as the code for the end of a
segment already does. A segment after the last point has no points
and gets the coefficients 0, like a segment before the first point.
Add a unit test.
Assisted-by: Claude Fable 5.2 (Anthropic)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/Makefile.am b/Makefile.am
index 1c07dee9..da397595 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1261,6 +1261,7 @@ check_PROGRAMS += params_model_test
endif # !DISABLED_LEGACY_ENGINE
check_PROGRAMS += progress_test
check_PROGRAMS += qrsequence_test
+check_PROGRAMS += quspline_test
check_PROGRAMS += ratngs_test
check_PROGRAMS += points_test
check_PROGRAMS += recodebeam_test
@@ -1588,6 +1589,10 @@ qrsequence_test_SOURCES = unittest/qrsequence_test.cc
qrsequence_test_CPPFLAGS = $(unittest_CPPFLAGS)
qrsequence_test_LDADD = $(TESS_LIBS)
+quspline_test_SOURCES = unittest/quspline_test.cc
+quspline_test_CPPFLAGS = $(unittest_CPPFLAGS)
+quspline_test_LDADD = $(TESS_LIBS)
+
ratngs_test_SOURCES = unittest/ratngs_test.cc
ratngs_test_CPPFLAGS = $(unittest_CPPFLAGS)
ratngs_test_LDADD = $(TESS_LIBS)
diff --git a/src/ccstruct/quspline.cpp b/src/ccstruct/quspline.cpp
index 7342fae4..91dc8f7a 100644
--- a/src/ccstruct/quspline.cpp
+++ b/src/ccstruct/quspline.cpp
@@ -103,7 +103,10 @@ QSPLINE::QSPLINE( // constructor
qlsq.clear();
/*first blob */
pointindex = ptcounts[segment];
- if (pointindex > 0 && xpts[pointindex] != xpts[pointindex - 1] &&
+ // A segment which starts after the last point has no point to
+ // interpolate with.
+ if (pointindex > 0 && pointindex < pointcount &&
+ xpts[pointindex] != xpts[pointindex - 1] &&
xpts[pointindex] != xstarts[segment]) {
qlsq.add(xstarts[segment],
ypts[pointindex - 1] + (ypts[pointindex] - ypts[pointindex - 1]) *
diff --git a/unittest/quspline_test.cc b/unittest/quspline_test.cc
new file mode 100644
index 00000000..b50faa59
--- /dev/null
+++ b/unittest/quspline_test.cc
@@ -0,0 +1,39 @@
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "quspline.h"
+
+#include "include_gunit.h"
+
+namespace tesseract {
+
+// Returns the value at x of a spline for points whose last segment starts
+// after the last point. The segment boundaries and the points are those of
+// the test case of OSS-Fuzz issue 591595917. The arrays with the points have
+// one more element than there are points. The spline must not depend on it.
+static double LastSegmentY(int unused, double x) {
+ int xstarts[] = {-310, -279, -246, -237, -208, -155, -113, -70, -21, -21};
+ int xpts[] = {-309, -251, -242, -234, -184, -128,
+ -100, -61, -22, -22, unused};
+ int ypts[] = {-159, -156, -159, -156, -159, -155,
+ -159, -155, -143, -155, unused};
+ QSPLINE spline(xstarts, 9, xpts, ypts, 10, 2);
+ return spline.y(x);
+}
+
+// The constructor used the element after the last point for a segment which
+// starts after the last point.
+TEST(QSplineTest, SegmentAfterLastPoint) {
+ // -21 is in the last segment, -22 in the one before it.
+ EXPECT_EQ(LastSegmentY(1000, -21), LastSegmentY(-1000, -21));
+ EXPECT_EQ(LastSegmentY(1000, -22), LastSegmentY(-1000, -22));
+}
+
+} // namespace tesseract