Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Defends against malformed preferences JSON file or Json content within the file #1171

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/simplnx/Core/Preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ constexpr int64 k_ReducedDataStructureSize = 3221225472; // 3 GB
constexpr int32 k_FailedToCreateDirectory_Code = -585;
constexpr int32 k_FileDoesNotExist_Code = -586;
constexpr int32 k_FileCouldNotOpen_Code = -587;
constexpr int32 k_JsonParseError_Code = -588;

constexpr StringLiteral k_FailedToCreateDirectory_Message = "Failed to the parent directory when saving Preferences";
constexpr StringLiteral k_FileDoesNotExist_Message = "Preferences file does not exist";
constexpr StringLiteral k_FileCouldNotOpen_Message = "Could not open Preferences file";
constexpr StringLiteral k_JsonParseError_Message = "Parsing the JSON Preferences file failed.";

std::filesystem::path getHomeDirectory()
{
Expand Down Expand Up @@ -230,7 +232,14 @@ Result<> Preferences::loadFromFile(const std::filesystem::path& filepath)
{
return MakeErrorResult(k_FileCouldNotOpen_Code, k_FileCouldNotOpen_Message);
}
m_Values = nlohmann::json::parse(fileStream);

nlohmann::json parsedResult = nlohmann::json::parse(fileStream, nullptr, false);
if(parsedResult.is_discarded())
{
return MakeErrorResult(k_JsonParseError_Code, k_JsonParseError_Message);
}

m_Values = parsedResult;

checkUseOoc();
return {};
Expand Down
Loading