fix: Crash when trying to read from an empty file or a directory
This commit is contained in:
parent
f3f1ac939a
commit
191a99f91b
@ -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();
|
||||
|
@ -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() };
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user