From b27c832cb7d946e59a2dfaedc117a92e0e37ecd7 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 29 Mar 2021 13:49:49 +0200 Subject: [PATCH] Add error log when trying to load bytes from file data that is too big --- Source/Engine/Platform/Base/FileBase.cpp | 39 ++++++++++++++++-------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/Source/Engine/Platform/Base/FileBase.cpp b/Source/Engine/Platform/Base/FileBase.cpp index d7308b0fd..9ff9149db 100644 --- a/Source/Engine/Platform/Base/FileBase.cpp +++ b/Source/Engine/Platform/Base/FileBase.cpp @@ -5,6 +5,7 @@ #include "Engine/Core/Types/String.h" #include "Engine/Core/Types/StringBuilder.h" #include "Engine/Core/Types/DataContainer.h" +#include "Engine/Core/Log.h" bool FileBase::ReadAllBytes(const StringView& path, byte* data, int32 length) { @@ -36,16 +37,23 @@ bool FileBase::ReadAllBytes(const StringView& path, Array& data) auto file = File::Open(path, FileMode::OpenExisting, FileAccess::Read, FileShare::All); if (file) { - uint32 size = file->GetSize(); - data.Resize(size, false); - if (size > 0) + const uint32 size = file->GetSize(); + if (size < MAX_int32) { - result = file->Read(data.Get(), size) != 0; + data.Resize(size, false); + if (size > 0) + { + result = file->Read(data.Get(), size) != 0; + } + else + { + data.Resize(0, false); + result = false; + } } else { - data.Resize(0, false); - result = false; + LOG(Error, "Failed to load file {0}. It's too big. Size: {1} MB", path, size / (1024 * 1024)); } Delete(file); @@ -61,16 +69,23 @@ bool FileBase::ReadAllBytes(const StringView& path, DataContainer& data) auto file = File::Open(path, FileMode::OpenExisting, FileAccess::Read, FileShare::All); if (file) { - uint32 size = file->GetSize(); - data.Allocate(size); - if (size > 0) + const uint32 size = file->GetSize(); + if (size < MAX_int32) { - result = file->Read(data.Get(), size) != 0; + data.Allocate(size); + if (size > 0) + { + result = file->Read(data.Get(), size) != 0; + } + else + { + data.Release(); + result = false; + } } else { - data.Release(); - result = false; + LOG(Error, "Failed to load file {0}. It's too big. Size: {1} MB", path, size / (1024 * 1024)); } Delete(file);