-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Fix crash when tessdata directory doesn't exist #4465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"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? |
|
Solution
Added two-layer protection in
addAvailableLanguages()
:Changes
std::filesystem::exists()
andstd::filesystem::is_directory()
checksrecursive_directory_iterator
in try-catch blockTesting
Impact
This fix improves robustness for users with incomplete Tesseract installations.
Contributing to Hacktoberfest 2025