Commit a7240347 for tesseract
commit a7240347c4b0bbc829a7e0b3566d583213be3c9a
Author: Stefan Weil <sw@weilnetz.de>
Date: Fri Nov 13 22:31:46 2026 +0100
osdetect: skip choices without a valid script id
ScriptDetector::detect_blob() used the script id of each choice as an
index without a check. The choice which Classify::AddLargeSpeckleTo()
adds when the classification fails has the id -1, so a page with a
large speckle made the code throw std::out_of_range, which is not
caught in the library.
Skip ids which are negative or not less than kMaxNumberOfScripts.
Fix proposed by the Anthropic OSS Scanner (finding ANT-2026-LDVI7LVQ).
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 95fa5c9a..07647d54 100644
--- a/src/ccmain/osdetect.cpp
+++ b/src/ccmain/osdetect.cpp
@@ -469,6 +469,11 @@ void ScriptDetector::detect_blob(BLOB_CHOICE_LIST *scores) {
for (choice_it.mark_cycle_pt(); !choice_it.cycled_list(); choice_it.forward()) {
BLOB_CHOICE *choice = choice_it.data();
int id = choice->script_id();
+ if (id < 0 || id >= kMaxNumberOfScripts) {
+ // No usable script, e.g. the fallback choice added by
+ // Classify::AddLargeSpeckleTo when classification failed.
+ continue;
+ }
if (allowed_scripts_ != nullptr && !allowed_scripts_->empty()) {
// Check that the choice is in an allowed script.
size_t s = 0;