Commit 12538c83 for tesseract

commit 12538c836b0b6996e29b28740d894005a4d5974c
Author: Stefan Weil <sw@weilnetz.de>
Date:   Fri Nov 13 21:26:30 2026 +0100

    ccstruct: allow more than 65535 characters in a REJMAP

    REJMAP kept its length in a uint16_t, and REJMAP::initialise() took a
    uint16_t, but both callers pass a 32 bit count. A word with more than
    65535 characters or blobs therefore got a reject map which was too
    short. Tesseract::write_results() checks the length with ASSERT_HOST
    and aborted the process, and GetUNLVText() failed the index check of
    the map.

    Use unsigned for the length and the index of the map and int for its
    counts, and adapt the users.

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

    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 728481c0..f74f7ee4 100644
--- a/src/api/baseapi.cpp
+++ b/src/api/baseapi.cpp
@@ -2853,7 +2853,7 @@ int TessBaseAPI::TextLength(int *blob_count) const {
     if (choice != nullptr) {
       total_blobs += choice->length() + 2;
       total_length += choice->unichar_string().length() + 2;
-      for (int i = 0; i < word->reject_map.length(); ++i) {
+      for (unsigned i = 0; i < word->reject_map.length(); ++i) {
         if (word->reject_map[i].rejected()) {
           ++total_length;
         }
diff --git a/src/ccmain/docqual.cpp b/src/ccmain/docqual.cpp
index 7861946f..b1f1f1fe 100644
--- a/src/ccmain/docqual.cpp
+++ b/src/ccmain/docqual.cpp
@@ -144,7 +144,7 @@ void Tesseract::unrej_good_quality_words( // unreject potential
   WERD_RES *word;
   ROW_RES *current_row;
   BLOCK_RES *current_block;
-  int i;
+  unsigned i;

   page_res_it.restart_page();
   while (page_res_it.word() != nullptr) {
@@ -244,16 +244,17 @@ void Tesseract::doc_and_block_rejection( // reject big chunks
         bool prev_word_rejected = false;
         while ((word = page_res_it.word()) != nullptr && (page_res_it.block() == current_block)) {
           bool rej_word;
+          const int word_len = word->reject_map.length();
           if (tessedit_preserve_blk_rej_perfect_wds) {
             rej_word = word->reject_map.reject_count() > 0 ||
-                       word->reject_map.length() < tessedit_preserve_min_wd_len;
+                       word_len < tessedit_preserve_min_wd_len;
             if (rej_word && tessedit_dont_blkrej_good_wds &&
-                word->reject_map.length() >= tessedit_preserve_min_wd_len &&
+                word_len >= tessedit_preserve_min_wd_len &&
                 acceptable_word_string(*word->uch_set, word->best_choice->unichar_string().c_str(),
                                        word->best_choice->unichar_lengths().c_str()) !=
                     AC_UNACCEPTABLE) {
               word_char_quality(word, &char_quality, &accepted_char_quality);
-              rej_word = char_quality != word->reject_map.length();
+              rej_word = char_quality != word_len;
             }
           } else {
             rej_word = true;
@@ -302,21 +303,21 @@ void Tesseract::doc_and_block_rejection( // reject big chunks
             while ((word = page_res_it.word()) != nullptr && page_res_it.row() == current_row) {
               /* Preserve words on good docs unless they are mostly rejected*/
               bool rej_word;
+              const int word_len = word->reject_map.length();
               if (!tessedit_row_rej_good_docs && good_quality_doc) {
-                rej_word = word->reject_map.reject_count() /
-                               static_cast<float>(word->reject_map.length()) >
+                rej_word = word->reject_map.reject_count() / static_cast<float>(word_len) >
                            tessedit_good_doc_still_rowrej_wd;
               } else if (tessedit_preserve_row_rej_perfect_wds) {
                 /* Preserve perfect words anyway */
                 rej_word = word->reject_map.reject_count() > 0 ||
-                           word->reject_map.length() < tessedit_preserve_min_wd_len;
+                           word_len < tessedit_preserve_min_wd_len;
                 if (rej_word && tessedit_dont_rowrej_good_wds &&
-                    word->reject_map.length() >= tessedit_preserve_min_wd_len &&
+                    word_len >= tessedit_preserve_min_wd_len &&
                     acceptable_word_string(
                         *word->uch_set, word->best_choice->unichar_string().c_str(),
                         word->best_choice->unichar_lengths().c_str()) != AC_UNACCEPTABLE) {
                   word_char_quality(word, &char_quality, &accepted_char_quality);
-                  rej_word = char_quality != word->reject_map.length();
+                  rej_word = char_quality != word_len;
                 }
               } else {
                 rej_word = true;
@@ -586,7 +587,7 @@ void Tesseract::tilde_delete(PAGE_RES_IT &page_res_it) {
 }

 void Tesseract::convert_bad_unlv_chs(WERD_RES *word_res) {
-  int i;
+  unsigned i;
   UNICHAR_ID unichar_dash = word_res->uch_set->unichar_to_id("-");
   UNICHAR_ID unichar_space = word_res->uch_set->unichar_to_id(" ");
   UNICHAR_ID unichar_tilde = word_res->uch_set->unichar_to_id("~");
diff --git a/src/ccmain/fixspace.cpp b/src/ccmain/fixspace.cpp
index 2bef190e..31e2b00a 100644
--- a/src/ccmain/fixspace.cpp
+++ b/src/ccmain/fixspace.cpp
@@ -289,7 +289,7 @@ int16_t Tesseract::eval_word_spacing(WERD_RES_LIST &word_res_list) {
   Yes IF it didn't end in a 1 when the first char of this word is a digit
     AND it didn't end in a digit when the first char of this word is a 1
 */
-      auto word_len = word->reject_map.length();
+      int word_len = word->reject_map.length();
       bool current_word_ok_so_far = false;
       if (!((prev_char_1 && digit_or_numeric_punct(word, 0)) ||
             (prev_char_digit &&
diff --git a/src/ccmain/output.cpp b/src/ccmain/output.cpp
index 788c33fc..a296bc43 100644
--- a/src/ccmain/output.cpp
+++ b/src/ccmain/output.cpp
@@ -249,7 +249,7 @@ char determine_newline_type( // test line ends
  * character which is repeated - as determined earlier by fix_rep_char()
  *************************************************************************/
 UNICHAR_ID Tesseract::get_rep_char(WERD_RES *word) { // what char is repeated?
-  int i;
+  unsigned i;
   for (i = 0; ((i < word->reject_map.length()) && (word->reject_map[i].rejected())); ++i) {
     ;
   }
@@ -338,7 +338,7 @@ void Tesseract::set_unlv_suspects(WERD_RES *word_res) {
     return;
   }

-  if (!suspect_constrain_1Il || (word_res->reject_map.length() <= suspect_short_words)) {
+  if (!suspect_constrain_1Il || (len <= suspect_short_words)) {
     for (i = 0; i < len; i++) {
       if (word_res->reject_map[i].rejected()) {
         if ((word_res->reject_map[i].flag(R_1IL_CONFLICT) ||
@@ -356,7 +356,7 @@ void Tesseract::set_unlv_suspects(WERD_RES *word_res) {
   if (acceptable_word_string(*word_res->uch_set, word.unichar_string().c_str(),
                              word.unichar_lengths().c_str()) != AC_UNACCEPTABLE ||
       acceptable_number_string(word.unichar_string().c_str(), word.unichar_lengths().c_str())) {
-    if (word_res->reject_map.length() > suspect_short_words) {
+    if (len > suspect_short_words) {
       for (i = 0; i < len; i++) {
         if (word_res->reject_map[i].rejected() && (!word_res->reject_map[i].perm_rejected() ||
                                                    word_res->reject_map[i].flag(R_1IL_CONFLICT) ||
diff --git a/src/ccmain/reject.cpp b/src/ccmain/reject.cpp
index 36746e88..ef425145 100644
--- a/src/ccmain/reject.cpp
+++ b/src/ccmain/reject.cpp
@@ -264,13 +264,13 @@ float compute_reject_threshold(WERD_CHOICE *word) {
 void Tesseract::reject_edge_blobs(WERD_RES *word) {
   TBOX word_box = word->word->bounding_box();
   // Use the box_word as it is already denormed back to image coordinates.
-  int blobcount = word->box_word->length();
+  unsigned blobcount = word->box_word->length();

   if (word_box.left() < tessedit_image_border || word_box.bottom() < tessedit_image_border ||
       word_box.right() + tessedit_image_border > ImageWidth() - 1 ||
       word_box.top() + tessedit_image_border > ImageHeight() - 1) {
     ASSERT_HOST(word->reject_map.length() == blobcount);
-    for (int blobindex = 0; blobindex < blobcount; blobindex++) {
+    for (unsigned blobindex = 0; blobindex < blobcount; blobindex++) {
       TBOX blob_box = word->box_word->BlobBox(blobindex);
       if (blob_box.left() < tessedit_image_border || blob_box.bottom() < tessedit_image_border ||
           blob_box.right() + tessedit_image_border > ImageWidth() - 1 ||
diff --git a/src/ccstruct/pageres.cpp b/src/ccstruct/pageres.cpp
index 4d096fd7..5efe21f3 100644
--- a/src/ccstruct/pageres.cpp
+++ b/src/ccstruct/pageres.cpp
@@ -1769,8 +1769,8 @@ WERD_RES *PAGE_RES_IT::forward_block() {
 }

 void PAGE_RES_IT::rej_stat_word() {
-  int16_t chars_in_word;
-  int16_t rejects_in_word = 0;
+  int chars_in_word;
+  int rejects_in_word = 0;

   chars_in_word = word_res->reject_map.length();
   page_res->char_count += chars_in_word;
diff --git a/src/ccstruct/rejctmap.cpp b/src/ccstruct/rejctmap.cpp
index e95c0694..6f646a3c 100644
--- a/src/ccstruct/rejctmap.cpp
+++ b/src/ccstruct/rejctmap.cpp
@@ -64,13 +64,13 @@ REJMAP &REJMAP::operator=(const REJMAP &source) {
   return *this;
 }

-void REJMAP::initialise(uint16_t length) {
+void REJMAP::initialise(unsigned length) {
   ptr = std::make_unique<REJ[]>(length);
   len = length;
 }

-int16_t REJMAP::accept_count() const { // How many accepted?
-  int16_t count = 0;
+int REJMAP::accept_count() const { // How many accepted?
+  int count = 0;
   for (unsigned i = 0; i < len; i++) {
     if (ptr[i].accepted()) {
       count++;
@@ -98,7 +98,7 @@ bool REJMAP::quality_recoverable_rejects() const { // Any potential rejs?
 }

 void REJMAP::remove_pos( // Cut out an element
-    uint16_t pos         // element to remove
+    unsigned pos         // element to remove
 ) {
   ASSERT_HOST(pos < len);
   ASSERT_HOST(len > 0);
diff --git a/src/ccstruct/rejctmap.h b/src/ccstruct/rejctmap.h
index fc4b9780..92dffd13 100644
--- a/src/ccstruct/rejctmap.h
+++ b/src/ccstruct/rejctmap.h
@@ -309,7 +309,7 @@ public:

 class REJMAP {
   std::unique_ptr<REJ[]> ptr; // ptr to the chars
-  uint16_t len = 0;           // Number of chars
+  unsigned len = 0;           // Number of chars

 public:
   REJMAP() = default;
@@ -321,27 +321,27 @@ public:
   REJMAP &operator=(const REJMAP &source);

   // Sets up the ptr array to length, whatever it was before.
-  void initialise(uint16_t length);
+  void initialise(unsigned length);

   REJ &operator[](         // access function
-      uint16_t index) const // map index
+      unsigned index) const // map index
   {
     ASSERT_HOST(index < len);
     return ptr[index]; // no bounds checks
   }

-  uint16_t length() const { // map length
+  unsigned length() const { // map length
     return len;
   }

-  int16_t accept_count() const; // How many accepted?
+  int accept_count() const;     // How many accepted?

-  int16_t reject_count() const { // How many rejects?
+  int reject_count() const {    // How many rejects?
     return len - accept_count();
   }

   // Cut out an element.
-  void remove_pos(uint16_t pos);
+  void remove_pos(unsigned pos);

   void print(FILE *fp) const;