Fix crash when tessdata directory doesn't exist#4465
Fix crash when tessdata directory doesn't exist#4465kdt523 wants to merge 2 commits intotesseract-ocr:mainfrom
Conversation
|
How did you get the crash? |
|
I get a exception which is handled: Isn't it better to get such an error message instead of silently failing? |
src/api/baseapi.cpp
Outdated
| // Check if directory exists before attempting to iterate | ||
| if (!std::filesystem::exists(datadir) || !std::filesystem::is_directory(datadir)) { | ||
| return; | ||
| } | ||
|
|
There was a problem hiding this comment.
I think this code block is not needed. The iteration will raise an exception for both bases, and this exception is handled.
src/api/baseapi.cpp
Outdated
| // Check if directory exists before attempting to iterate | ||
| if (!std::filesystem::exists(datadir) || !std::filesystem::is_directory(datadir)) { | ||
| return; | ||
| } | ||
|
|
There was a problem hiding this comment.
| // Check if directory exists before attempting to iterate | |
| if (!std::filesystem::exists(datadir) || !std::filesystem::is_directory(datadir)) { | |
| return; | |
| } | |
There was a problem hiding this comment.
Done Removed the existence check as suggested. The code now uses only the try-catch approach to handle filesystem errors, which is cleaner and still prevents the crash
src/api/baseapi.cpp
Outdated
| } | ||
| } | ||
| } catch (const std::filesystem::filesystem_error&) { | ||
| // Silently handle filesystem errors (e.g., permission denied, corrupted filesystem) |
There was a problem hiding this comment.
"Permission denied" was already silently handled.
The crash was reported in a GitHub issue with a complete stack trace. I didn't personally reproduce it, but analyzed the code to understand the root cause: The crash happens when: User installs tesseract-ocr package without language data packages |
Are you referring to issue #4364? |
|
|
i made required changes could you please approve it |
|
Wait. What is the program that showed crash in #4364? From #4364
It is not libtesseract issue, you should handle C++ exceptions. And it is known that Qt does not use C++ exceptions, so it might be something new for a programmer, but tess calls must be wrapped with try..catch in that user program. |
|
In C++ with exceptions we consider every line "correct" without any double checks or paranoid checks. |
Solution
Added two-layer protection in
addAvailableLanguages():Changes
std::filesystem::exists()andstd::filesystem::is_directory()checksrecursive_directory_iteratorin try-catch blockTesting
Impact
This fix improves robustness for users with incomplete Tesseract installations.
Contributing to Hacktoberfest 2025