Commit e745d387 for tesseract

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

    Stop at a cyclic chain of pages in a multipage TIFF file

    ProcessPagesMultipageTiff() reads one page after the other and gets
    the offset of the next image file directory from the file. A file in
    which that chain points back to an earlier directory made the loop
    recognize the same pages forever. MasterTrainer::LoadPageImages() has
    the same loop.

    Remember the offsets which were already read, and handle a repeated
    offset as the end of the document with a warning.

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

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

diff --git a/src/api/baseapi.cpp b/src/api/baseapi.cpp
index 7f3f67ca..728481c0 100644
--- a/src/api/baseapi.cpp
+++ b/src/api/baseapi.cpp
@@ -73,7 +73,7 @@
 #include <iostream> // for std::cin
 #include <locale>   // for std::locale::classic
 #include <memory>   // for std::unique_ptr
-#include <set>      // for std::pair
+#include <set>      // for std::set
 #include <sstream>  // for std::stringstream
 #include <string_view>
 #include <vector>   // for std::vector
@@ -1010,6 +1010,8 @@ bool TessBaseAPI::ProcessPagesMultipageTiff(const l_uint8 *data, size_t size, co
   Pix *pix = nullptr;
   int page = (tessedit_page_number >= 0) ? tessedit_page_number : 0;
   size_t offset = 0;
+  // Offsets of the image file directories which were already read.
+  std::set<size_t> seen_offsets;
   for (;; ++page) {
     if (tessedit_page_number >= 0) {
       page = tessedit_page_number;
@@ -1038,6 +1040,12 @@ bool TessBaseAPI::ProcessPagesMultipageTiff(const l_uint8 *data, size_t size, co
     if (!offset) {
       break;
     }
+    // The offset comes from the file: a repeated one would loop forever,
+    // so treat a cyclic chain of pages as the end of the document.
+    if (!seen_offsets.insert(offset).second) {
+      tprintf("Warning, TIFF file %s has a cyclic chain of pages\n", filename);
+      break;
+    }
   }
   return true;
 }
diff --git a/src/training/common/mastertrainer.cpp b/src/training/common/mastertrainer.cpp
index f8dddd91..193859ef 100644
--- a/src/training/common/mastertrainer.cpp
+++ b/src/training/common/mastertrainer.cpp
@@ -23,6 +23,7 @@

 #include <cmath>
 #include <ctime>
+#include <set>
 #include "boxread.h"
 #include "classify.h"
 #include "errorcounter.h"
@@ -225,6 +226,8 @@ void MasterTrainer::AddSample(bool verification, const char *unichar,
 // is used as an offset for page numbers in the samples.
 void MasterTrainer::LoadPageImages(const char *filename) {
   size_t offset = 0;
+  // Offsets of the image file directories which were already read.
+  std::set<size_t> seen_offsets;
   int page;
   Image pix;
   for (page = 0;; page++) {
@@ -236,6 +239,12 @@ void MasterTrainer::LoadPageImages(const char *filename) {
     if (!offset) {
       break;
     }
+    // The offset comes from the file: a repeated one would loop forever,
+    // so treat a cyclic chain of pages as the end of the document.
+    if (!seen_offsets.insert(offset).second) {
+      tprintf("Warning, TIFF file %s has a cyclic chain of pages\n", filename);
+      break;
+    }
   }
   tprintf("Loaded %d page images from %s\n", page, filename);
 }