From 979da49d1ddab29a7c5feb9ea61d42fcc9d5c085 Mon Sep 17 00:00:00 2001 From: Michael Adler Date: Mon, 20 Dec 2021 11:37:47 +0100 Subject: [PATCH] Fix crash on inaccessible directory (#14) qmarkdown crashes if a directory with inaccessible permissions (e.g. owner root, mode 0700) is encountered while scanning the workspace. The crash is caused by an exception which is thrown by the fs::directory_iterator. --- src/fileinput.cpp | 4 +++- src/resgen.cpp | 12 +++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/fileinput.cpp b/src/fileinput.cpp index ba34f96..6d1a447 100644 --- a/src/fileinput.cpp +++ b/src/fileinput.cpp @@ -46,7 +46,9 @@ void FileInput::auto_complete() { count = 0; if (directory_exists(q_path)) { - for (const auto &entry : FILESYSTEM::directory_iterator(q_path->toStdString())) { + for (auto it = FILESYSTEM::recursive_directory_iterator(q_path->toStdString(), FILESYSTEM::directory_options::skip_permission_denied); it != FILESYSTEM::recursive_directory_iterator(); ++it) { + if (it.depth() > 0) continue; + const auto entry = *it; string entry_str = entry.path(); size_t e_slash = entry_str.rfind("/") + 1; diff --git a/src/resgen.cpp b/src/resgen.cpp index 058f814..4e445da 100644 --- a/src/resgen.cpp +++ b/src/resgen.cpp @@ -4,11 +4,9 @@ #if __has_include() #include #define FILESYSTEM filesystem -#define IS_DIRECTORY(p) p.is_directory() #elif __has_include() #include #define FILESYSTEM std::experimental::filesystem -#define IS_DIRECTORY(p) FILESYSTEM::is_directory(p.symlink_status()) #endif #include @@ -16,10 +14,10 @@ using namespace std; -void read_directory(ofstream *outfile, int depth, QString path, bool *status) { - for (const auto &entry : FILESYSTEM::directory_iterator(path.toStdString())) { - if (IS_DIRECTORY(entry) && depth < 3) - read_directory(outfile, depth + 1, entry.path().c_str(), status); +void read_directory(ofstream *outfile, QString path, bool *status) { + for (auto it = FILESYSTEM::recursive_directory_iterator(path.toStdString(), FILESYSTEM::directory_options::skip_permission_denied); it != FILESYSTEM::recursive_directory_iterator(); ++it) { + if (it.depth() > 3) continue; + const auto entry = *it; const QString e_path = entry.path().c_str(); const QString image_types[] = {".png", ".jpg", ".jpeg", ".gif"}; for (QString type : image_types) { @@ -42,7 +40,7 @@ void res_gen() { outfile << "" << endl; bool image_exists = false; - read_directory(&outfile, 0, ".", &image_exists); + read_directory(&outfile, ".", &image_exists); outfile << "" << endl; outfile << "" << endl;