From 959a404e1c6fe06428fa0788e35fb0fba32d5dc6 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 1 Feb 2024 22:35:24 +0100 Subject: [PATCH] fix: Don't pass nullptrs to libmagic #1485 --- lib/libimhex/source/helpers/magic.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/libimhex/source/helpers/magic.cpp b/lib/libimhex/source/helpers/magic.cpp index 6a5ac8154..51bf139f1 100644 --- a/lib/libimhex/source/helpers/magic.cpp +++ b/lib/libimhex/source/helpers/magic.cpp @@ -96,6 +96,8 @@ namespace hex::magic { } std::string getDescription(const std::vector &data, bool firstEntryOnly) { + if (data.empty()) return ""; + auto magicFiles = getMagicFiles(); if (magicFiles.has_value()) { @@ -119,6 +121,8 @@ namespace hex::magic { } std::string getMIMEType(const std::vector &data, bool firstEntryOnly) { + if (data.empty()) return ""; + auto magicFiles = getMagicFiles(); if (magicFiles.has_value()) { @@ -134,6 +138,13 @@ namespace hex::magic { return ""; } + std::string getMIMEType(prv::Provider *provider, size_t size, bool firstEntryOnly) { + std::vector buffer(std::min(provider->getSize(), size), 0x00); + provider->read(provider->getBaseAddress(), buffer.data(), buffer.size()); + + return getMIMEType(buffer, firstEntryOnly); + } + std::string getExtensions(prv::Provider *provider, size_t size, bool firstEntryOnly) { std::vector buffer(std::min(provider->getSize(), size), 0x00); provider->read(provider->getBaseAddress(), buffer.data(), buffer.size()); @@ -142,6 +153,8 @@ namespace hex::magic { } std::string getExtensions(const std::vector &data, bool firstEntryOnly) { + if (data.empty()) return ""; + auto magicFiles = getMagicFiles(); if (magicFiles.has_value()) { @@ -165,6 +178,8 @@ namespace hex::magic { } std::string getAppleCreatorType(const std::vector &data, bool firstEntryOnly) { + if (data.empty()) return ""; + auto magicFiles = getMagicFiles(); if (magicFiles.has_value()) { @@ -180,13 +195,6 @@ namespace hex::magic { return {}; } - std::string getMIMEType(prv::Provider *provider, size_t size, bool firstEntryOnly) { - std::vector buffer(std::min(provider->getSize(), size), 0x00); - provider->read(provider->getBaseAddress(), buffer.data(), buffer.size()); - - return getMIMEType(buffer, firstEntryOnly); - } - bool isValidMIMEType(const std::string &mimeType) { // MIME types always contain a slash if (!mimeType.contains("/"))