Skip to content

Commit

Permalink
BUG: Defends against malformed preferences JSON file or Json content …
Browse files Browse the repository at this point in the history
…within the file (#1171)

Signed-off-by: Michael Jackson <[email protected]>
  • Loading branch information
imikejackson authored Jan 10, 2025
1 parent 3ccfd99 commit c605f3a
Showing 1 changed file with 10 additions and 1 deletion.
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

0 comments on commit c605f3a

Please sign in to comment.