Make JSON reading safer

- Check that the file actually opened before reading it
- If the JSON was discarded, then make just use an empty object
This commit is contained in:
Pk11
2021-03-23 04:47:49 -05:00
parent 5343661069
commit 77edd99749
7 changed files with 65 additions and 39 deletions
+8 -4
View File
@@ -113,11 +113,15 @@ UniStoreInfo GetInfo(const std::string &file, const std::string &fileName) {
if(*(u32*)(fileName.c_str() + fileName.length() - 4) == (1886349435 & ~(1 << 3))) return Temp;
}
nlohmann::json JSON = nullptr;
nlohmann::json JSON;
FILE *temp = fopen(file.c_str(), "rt");
if(temp) {
JSON = nlohmann::json::parse(temp, nullptr, false);
fclose(temp);
}
if (JSON.is_discarded())
JSON = { };
FILE *temp = fopen(file.c_str(), "r");
JSON = nlohmann::json::parse(temp, nullptr, false);
fclose(temp);
if (!JSON.contains("storeInfo")) return Temp; // storeInfo does not exist.