Commit 212a238d for tesseract

commit 212a238d2e9ec38483a6d50a932b2f9ab9604322
Author: Stefan Weil <sw@weilnetz.de>
Date:   Fri Nov 13 22:31:46 2026 +0100

    Ignore UZN zones which are not inside of the image

    read_unlv_file() took the zones from the file <image>.uzn without
    comparing them with the size of the image. A zone with a negative x
    or one which extends beyond the image gave a block outside of the
    image, and block_edges() wrote behind two heap arrays, for a length
    which the zone file sets.

    Check each zone against the width and the height of the image and
    ignore it with a warning if it does not fit. Also assert in
    block_edges() that the block does not start at negative coordinates.

    Fix proposed by the Anthropic OSS Scanner (finding ANT-2026-CH5ZX325).

    Assisted-by: OpenCode / Claude Fable 5.2 (Anthropic)
    Signed-off-by: Stefan Weil <sw@weilnetz.de>

diff --git a/src/ccmain/osdetect.cpp b/src/ccmain/osdetect.cpp
index 07647d54..b363d738 100644
--- a/src/ccmain/osdetect.cpp
+++ b/src/ccmain/osdetect.cpp
@@ -187,7 +187,7 @@ int orientation_and_script_detection(const char *filename, OSResults *osr,
   int height = pixGetHeight(tess->pix_binary());

   BLOCK_LIST blocks;
-  if (!read_unlv_file(name, height, &blocks)) {
+  if (!read_unlv_file(name, width, height, &blocks)) {
     FullPageBlock(width, height, &blocks);
   }

diff --git a/src/ccmain/pagesegmain.cpp b/src/ccmain/pagesegmain.cpp
index dfe88f79..0e718665 100644
--- a/src/ccmain/pagesegmain.cpp
+++ b/src/ccmain/pagesegmain.cpp
@@ -112,7 +112,7 @@ int Tesseract::SegmentPage(const char *input_file, BLOCK_LIST *blocks, Tesseract
     if (lastdot != std::string::npos) {
       name.resize(lastdot);
     }
-    read_unlv_file(name, height, blocks);
+    read_unlv_file(name, width, height, blocks);
   }
   if (blocks->empty()) {
     // No UNLV file present. Work according to the PageSegMode.
diff --git a/src/ccstruct/blread.cpp b/src/ccstruct/blread.cpp
index fd851ca4..1cd9d8c4 100644
--- a/src/ccstruct/blread.cpp
+++ b/src/ccstruct/blread.cpp
@@ -35,6 +35,7 @@ namespace tesseract {

 bool read_unlv_file(   // print list of sides
     std::string &name, // basename of file
+    int32_t xsize,     // image size
     int32_t ysize,     // image size
     BLOCK_LIST *blocks // output list
 ) {
@@ -51,6 +52,13 @@ bool read_unlv_file(   // print list of sides
     return false; // didn't read one
   } else {
     while (tfscanf(pdfp, "%d %d %d %d %*s", &x, &y, &width, &height) >= 4) {
+      // The zone must be inside the image.
+      if (x < 0 || y < 0 || width <= 0 || height <= 0 || width > xsize - x ||
+          height > ysize - y) {
+        tprintf("Warning: ignoring UZN zone %d %d %d %d, not inside the image\n", x, y, width,
+                height);
+        continue;
+      }
       // make rect block
       block = new BLOCK(name.c_str(), true, 0, 0, static_cast<int16_t>(x),
                         static_cast<int16_t>(ysize - y - height), static_cast<int16_t>(x + width),
diff --git a/src/ccstruct/blread.h b/src/ccstruct/blread.h
index e57d1f68..9876e870 100644
--- a/src/ccstruct/blread.h
+++ b/src/ccstruct/blread.h
@@ -28,6 +28,7 @@ class BLOCK_LIST;

 bool read_unlv_file(   // print list of sides
     std::string &name, // basename of file
+    int32_t xsize,     // image size
     int32_t ysize,     // image size
     BLOCK_LIST *blocks // output list
 );
diff --git a/src/textord/scanedg.cpp b/src/textord/scanedg.cpp
index 095f80da..5835e18f 100644
--- a/src/textord/scanedg.cpp
+++ b/src/textord/scanedg.cpp
@@ -73,6 +73,8 @@ void block_edges(Image t_pix,   // thresholded image
   CRACKEDGE *free_cracks = nullptr;

   block->bounding_box(bleft, tright); // block box
+  ASSERT_HOST(bleft.x() >= 0);
+  ASSERT_HOST(bleft.y() >= 0);
   ASSERT_HOST(tright.x() <= width);
   ASSERT_HOST(tright.y() <= height);
   int block_width = tright.x() - bleft.x();