From 86fdd531dae18f6dcfff905af1f08222ccf33c25 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 12 Nov 2020 23:08:17 +0100 Subject: [PATCH] Fix crash if no magic folder is present --- source/views/view_information.cpp | 37 ++++++++++++++++++------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/source/views/view_information.cpp b/source/views/view_information.cpp index c8cc4317c..406fd285e 100644 --- a/source/views/view_information.cpp +++ b/source/views/view_information.cpp @@ -83,35 +83,40 @@ namespace hex { std::string magicFiles; - for (const auto &entry : std::filesystem::directory_iterator("magic")) { + std::error_code error; + for (const auto &entry : std::filesystem::directory_iterator("magic", error)) { if (entry.is_regular_file() && entry.path().extension() == ".mgc") magicFiles += entry.path().string() + MAGIC_PATH_SEPARATOR; } - magicFiles.pop_back(); - { - magic_t cookie = magic_open(MAGIC_NONE); - if (magic_load(cookie, magicFiles.c_str()) == -1) + if (!error) { + magicFiles.pop_back(); + + { + magic_t cookie = magic_open(MAGIC_NONE); + if (magic_load(cookie, magicFiles.c_str()) == -1) goto skip_description; - this->m_fileDescription = magic_buffer(cookie, buffer.data(), buffer.size()); + this->m_fileDescription = magic_buffer(cookie, buffer.data(), buffer.size()); - skip_description: + skip_description: - magic_close(cookie); - } + magic_close(cookie); + } - { - magic_t cookie = magic_open(MAGIC_MIME); - if (magic_load(cookie, magicFiles.c_str()) == -1) - goto skip_mime; + { + magic_t cookie = magic_open(MAGIC_MIME); + if (magic_load(cookie, magicFiles.c_str()) == -1) + goto skip_mime; - this->m_mimeType = magic_buffer(cookie, buffer.data(), buffer.size()); + this->m_mimeType = magic_buffer(cookie, buffer.data(), buffer.size()); - skip_mime: + skip_mime: + + magic_close(cookie); + } - magic_close(cookie); }