Commit d663db0d for tesseract

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

    thresholder: fail if the colormap of an image cannot be removed

    ImageThresholder::ThresholdToPix() did not check the result of
    pixRemoveColormap(). A colormap with colors turns an image with 8 bits
    per pixel into one with 32, and Leptonica refuses images of 2^31
    bytes or more. So a large image with a colormap, which is still
    within the size limit of Tesseract, gave a null pointer, and
    OtsuThreshold() then wrote to an empty vector (SIGSEGV).

    Return false with a message, like for an image which is too large.

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

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

diff --git a/src/ccmain/thresholder.cpp b/src/ccmain/thresholder.cpp
index 4fb2af35..7aa142b1 100644
--- a/src/ccmain/thresholder.cpp
+++ b/src/ccmain/thresholder.cpp
@@ -303,6 +303,12 @@ bool ImageThresholder::ThresholdToPix(Image *pix) {
   Image src = pix_;
   if (pixGetColormap(src)) {
     src = pixRemoveColormap(src, REMOVE_CMAP_BASED_ON_SRC);
+    if (src == nullptr) {
+      // A colormap with non-grey entries expands to 32 bpp, which can exceed
+      // the Leptonica image size limit even when the 8 bpp source fits.
+      tprintf("Failed to remove colormap: (%d, %d)\n", image_width_, image_height_);
+      return false;
+    }
   }
   OtsuThresholdRectToPix(src, pix);
   if (src != pix_) {