1
0
mirror of synced 2025-02-17 18:59:21 +01:00

fix: Crash when trying to read from an empty file or a directory

This commit is contained in:
WerWolv 2022-02-26 16:43:38 +01:00
parent f3f1ac939a
commit 191a99f91b
2 changed files with 10 additions and 2 deletions

View File

@ -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();

View File

@ -57,6 +57,8 @@ namespace hex {
if (!isValid()) return {};
auto size = numBytes ?: getSize();
if (size == 0) return {};
std::vector<u8> 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<char *>(bytes.data()), bytes.size() };
}