Merge pull request #3999 from aws-lumberyard-dev/Prefab/IncreaseReadFileSizeLimit

Set max file size limits for AZ::JsonSerializationUtils::ReadJsonFile and AZ::Utils::ReadFile
This commit is contained in:
AMZN-koppersr
2021-09-09 10:33:40 -07:00
committed by GitHub
12 changed files with 37 additions and 32 deletions
@@ -240,11 +240,6 @@ namespace AZ
{
IO::SizeType length = stream.GetLength();
if (length > AZ::Utils::DefaultMaxFileSize)
{
return AZ::Failure(AZStd::string{ "Data is too large." });
}
AZStd::vector<char> memoryBuffer;
memoryBuffer.resize_no_construct(static_cast<AZStd::vector<char>::size_type>(static_cast<AZStd::vector<char>::size_type>(length) + 1));
@@ -259,12 +254,12 @@ namespace AZ
return ReadJsonString(AZStd::string_view{memoryBuffer.data(), memoryBuffer.size()});
}
AZ::Outcome<rapidjson::Document, AZStd::string> ReadJsonFile(AZStd::string_view filePath)
AZ::Outcome<rapidjson::Document, AZStd::string> ReadJsonFile(AZStd::string_view filePath, size_t maxFileSize)
{
// Read into memory first and then parse the json, rather than passing a file stream to rapidjson.
// This should avoid creating a large number of micro-reads from the file.
auto readResult = AZ::Utils::ReadFile<AZStd::string>(filePath);
auto readResult = AZ::Utils::ReadFile<AZStd::string>(filePath, maxFileSize);
if(!readResult.IsSuccess())
{
return AZ::Failure(readResult.GetError());
@@ -70,8 +70,10 @@ namespace AZ
//! Parse json text. Returns a failure with error message if the content is not valid JSON.
AZ::Outcome<rapidjson::Document, AZStd::string> ReadJsonString(AZStd::string_view jsonText);
//! Parse a json file. Returns a failure with error message if the content is not valid JSON.
AZ::Outcome<rapidjson::Document, AZStd::string> ReadJsonFile(AZStd::string_view filePath);
//! Parse a json file. Returns a failure with error message if the content is not valid JSON or if
//! the file size is larger than the max file size provided.
AZ::Outcome<rapidjson::Document, AZStd::string> ReadJsonFile(
AZStd::string_view filePath, size_t maxFileSize = AZStd::numeric_limits<size_t>::max());
//! Parse a json stream. Returns a failure with error message if the content is not valid JSON.
AZ::Outcome<rapidjson::Document, AZStd::string> ReadJsonStream(IO::GenericStream& stream);
+2 -5
View File
@@ -22,10 +22,6 @@ namespace AZ
{
namespace Utils
{
//! Protects from allocating too much memory. The choice of a 1MB threshold is arbitrary.
//! If you need to work with larger files, please use AZ::IO directly instead of these utility functions.
inline constexpr size_t DefaultMaxFileSize = 1024 * 1024;
//! Terminates the application without going through the shutdown procedure.
//! This is used when due to abnormal circumstances the application can no
//! longer continue. On most platforms and in most configurations this will
@@ -115,6 +111,7 @@ namespace AZ
//! Read a file into a string. Returns a failure with error message if the content could not be loaded or if
//! the file size is larger than the max file size provided.
template<typename Container = AZStd::string>
AZ::Outcome<Container, AZStd::string> ReadFile(AZStd::string_view filePath, size_t maxFileSize = DefaultMaxFileSize);
AZ::Outcome<Container, AZStd::string> ReadFile(
AZStd::string_view filePath, size_t maxFileSize = AZStd::numeric_limits<size_t>::max());
}
}