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
+7 -9
View File
@@ -110,9 +110,13 @@ Config::Config() {
this->initialize();
}
FILE *file = fopen("sdmc:/3ds/Universal-Updater/Config.json", "r");
this->json = nlohmann::json::parse(file, nullptr, false);
fclose(file);
FILE *file = fopen("sdmc:/3ds/Universal-Updater/Config.json", "rt");
if (file) {
this->json = nlohmann::json::parse(file, nullptr, false);
fclose(file);
}
if (this->json.is_discarded())
this->json = { };
/* Let us create a new one. */
if (!this->json.contains("Version")) this->initialize();
@@ -174,34 +178,28 @@ void Config::save() {
/* Helper functions. */
bool Config::getBool(const std::string &key) {
if (this->json.is_discarded()) return false;
if (!this->json.contains(key)) return false;
return this->json.at(key).get_ref<const bool &>();
}
void Config::setBool(const std::string &key, bool v) {
if (this->json.is_discarded()) return;
this->json[key] = v;
};
int Config::getInt(const std::string &key) {
if (this->json.is_discarded()) return 0;
if (!this->json.contains(key)) return 0;
return this->json.at(key).get_ref<const int64_t &>();
}
void Config::setInt(const std::string &key, int v) {
if (this->json.is_discarded()) return;
this->json[key] = v;
};
std::string Config::getString(const std::string &key) {
if (this->json.is_discarded()) return "";
if (!this->json.contains(key)) return "";
return this->json.at(key).get_ref<const std::string &>();
}
void Config::setString(const std::string &key, const std::string &v) {
if (this->json.is_discarded()) return;
this->json[key] = v;
};
+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.
+12 -4
View File
@@ -42,14 +42,22 @@ void Lang::load(const std::string &lang) {
/* Check if exist. */
if (access(("romfs:/lang/" + lang + "/app.json").c_str(), F_OK) == 0) {
values = fopen(("romfs:/lang/" + lang + "/app.json").c_str(), "rt");
appJson = nlohmann::json::parse(values, nullptr, false);
fclose(values);
if (values) {
appJson = nlohmann::json::parse(values, nullptr, false);
fclose(values);
}
if (appJson.is_discarded())
appJson = { };
return;
} else {
values = fopen("romfs:/lang/en/app.json", "rt");
appJson = nlohmann::json::parse(values, nullptr, false);
fclose(values);
if (values) {
appJson = nlohmann::json::parse(values, nullptr, false);
fclose(values);
}
if (appJson.is_discarded())
appJson = { };
return;
}
}