Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/api/baseapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,24 @@ static void ExtractFontName(const char* filename, std::string* fontname) {
*/
static void addAvailableLanguages(const std::string &datadir,
std::vector<std::string> *langs) {
for (const auto& entry :
std::filesystem::recursive_directory_iterator(datadir,
std::filesystem::directory_options::follow_directory_symlink |
std::filesystem::directory_options::skip_permission_denied)) {
auto path = entry.path().lexically_relative(datadir);
if (path.extension() == ".traineddata") {
langs->push_back(path.replace_extension("").string());
// Check if directory exists before attempting to iterate
if (!std::filesystem::exists(datadir) || !std::filesystem::is_directory(datadir)) {
return;
}

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Check if directory exists before attempting to iterate
if (!std::filesystem::exists(datadir) || !std::filesystem::is_directory(datadir)) {
return;
}

Copy link
Author

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

try {
for (const auto& entry :
std::filesystem::recursive_directory_iterator(datadir,
std::filesystem::directory_options::follow_directory_symlink |
std::filesystem::directory_options::skip_permission_denied)) {
auto path = entry.path().lexically_relative(datadir);
if (path.extension() == ".traineddata") {
langs->push_back(path.replace_extension("").string());
}
}
} catch (const std::filesystem::filesystem_error&) {
// Silently handle filesystem errors (e.g., permission denied, corrupted filesystem)
Copy link
Member

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 function will return with whatever languages were found so far
}
}

Expand Down