diff --git a/lib/libimhex/include/hex/helpers/file.hpp b/lib/libimhex/include/hex/helpers/file.hpp index db826f4df..c43c14eb1 100644 --- a/lib/libimhex/include/hex/helpers/file.hpp +++ b/lib/libimhex/include/hex/helpers/file.hpp @@ -20,7 +20,8 @@ namespace hex { class File { public: - enum class Mode { + enum class Mode + { Read, Write, Create @@ -36,7 +37,9 @@ namespace hex { File &operator=(File &&other) noexcept; - [[nodiscard]] bool isValid() const { return this->m_file != nullptr; } + [[nodiscard]] bool isValid() const { + return this->m_file != nullptr && fs::exists(this->m_path) && !fs::is_directory(this->m_path); + } void seek(u64 offset); void close(); diff --git a/lib/libimhex/source/helpers/file.cpp b/lib/libimhex/source/helpers/file.cpp index 49e9854a4..8dc276979 100644 --- a/lib/libimhex/source/helpers/file.cpp +++ b/lib/libimhex/source/helpers/file.cpp @@ -57,6 +57,8 @@ namespace hex { if (!isValid()) return {}; auto size = numBytes ?: getSize(); + if (size == 0) return {}; + std::vector bytes(size); auto bytesRead = fread(bytes.data(), 1, bytes.size(), this->m_file); @@ -72,6 +74,9 @@ namespace hex { auto bytes = readBytes(numBytes); + if (bytes.empty()) + return ""; + return { reinterpret_cast(bytes.data()), bytes.size() }; }