Add some sanity check for json access

When json is const, access to non-existant key causes assert failure. Need to check first if the key exists
sql-rework
Tomasz Golinski 2020-03-02 22:58:14 +01:00
rodzic 92bfb82eb2
commit 0fbdd6e3e9
1 zmienionych plików z 8 dodań i 4 usunięć

Wyświetl plik

@ -147,10 +147,12 @@ bool OCdb::update_cache(const json& j) {
std::map<std::string, short> fields2;
int res;
if (!j.count("object_key") || !j["object_key"].count("code") || !j.count("data")) return 0;
std::string code = j["object_key"]["code"].get<std::string>();
if (j["data"]["names"].count("pl") > 0 && !j["data"]["names"]["pl"].is_null())
if (j["data"].count("names") && j["data"]["names"].count("pl") && !j["data"]["names"]["pl"].is_null())
fields["name"] = j["data"]["names"]["pl"].get<std::string>();
else if (j["data"]["names"].count("en") > 0 && !j["data"]["names"]["en"].is_null())
else if (j["data"].count("names") && j["data"]["names"].count("en") && !j["data"]["names"]["en"].is_null())
fields["name"] = j["data"]["names"]["en"].get<std::string>();
if (j["data"].count("location") > 0 && !j["data"]["location"].is_null())
fields["location"] = j["data"]["location"].get<std::string>();
@ -170,7 +172,7 @@ bool OCdb::update_cache(const json& j) {
fields["region"] = j["data"]["region"].get<std::string>();
else if (j["data"].count("state") > 0 && !j["data"]["state"].is_null())
fields["region"] = j["data"]["state"].get<std::string>();
if (j["data"]["owner"].count("uuid") > 0 && !j["data"]["owner"]["uuid"].is_null())
if (j["data"].count("owner") && j["data"]["owner"].count("uuid") && !j["data"]["owner"]["uuid"].is_null())
fields["owner"] = j["data"]["owner"]["uuid"].get<std::string>();
if (fields.empty() && fields2.empty())
@ -225,12 +227,14 @@ bool OCdb::update_log(const json& j) {
std::map<std::string, short> fields2;
int res;
if (!j.count("object_key") || !j["object_key"].count("uuid") || !j.count("data")) return 0;
std::string uuid = j["object_key"]["uuid"].get<std::string>();
if (j["data"].count("cache_code") > 0 && !j["data"]["cache_code"].is_null())
fields["cache_code"] = j["data"]["cache_code"].get<std::string>();
if (j["data"].count("date") > 0 && !j["data"]["date"].is_null())
fields["date"] = j["data"]["date"].get<std::string>();
if (j["data"]["user"].count("uuid") > 0 && !j["data"]["user"]["uuid"].is_null())
if (j["data"].count("user") && j["data"]["user"].count("uuid") && !j["data"]["user"]["uuid"].is_null())
fields["user"] = j["data"]["user"]["uuid"].get<std::string>();
if (j["data"].count("type") > 0 && !j["data"]["type"].is_null()) {
if (j["data"]["type"].get<std::string>() == "Didn't find it")