commit fc6936c9e0f166ad38902a6d7e68571f109129ba from: the xhr date: Thu Sep 16 12:11:09 2021 UTC Be more careful when opening JSON files commit - dc9d952128ad03ad2dc1f2d1b93fa54394757e9c commit + fc6936c9e0f166ad38902a6d7e68571f109129ba blob - 950d587186fe7c0e081ef6da79fe617883985c9c blob + 3f9e0d7ff617a0a991093d4e4748b60b9e002c9f --- character.c +++ character.c @@ -467,7 +467,12 @@ save_character() json_object_object_add(root, "characters", items); } else { /* Get existing character array from JSON */ - items = json_object_object_get(root, "characters"); + if (!json_object_object_get_ex(root, "characters", &items)) { + log_debug("Cannot find a [characters] array in %s\n", path); + items = json_object_new_array(); + json_object_object_add(root, "characters", items); + } + temp_n = json_object_array_length(items); for (i = 0; i < temp_n; i++) { json_object *temp = json_object_array_get_idx(items, i); @@ -507,7 +512,12 @@ delete_saved_character(int id) return; } - json_object *characters = json_object_object_get(root, "characters"); + json_object *characters; + if (!json_object_object_get_ex(root, "characters", &characters)) { + log_debug("Cannot find a [characters] array in %s\n", path); + return; + } + temp_n = json_object_array_length(characters); for (i = 0; i < temp_n; i++) { json_object *temp = json_object_array_get_idx(characters, i); @@ -590,7 +600,12 @@ load_character(int id) if ((c->fight = calloc(1, sizeof(struct fight))) == NULL) log_errx(1, "calloc"); - json_object *characters = json_object_object_get(root, "characters"); + json_object *characters; + if (!json_object_object_get_ex(root, "characters", &characters)) { + log_debug("Cannot find a [characters] array in %s\n", path); + return -1; + } + temp_n = json_object_array_length(characters); for (i=0; i < temp_n; i++) { json_object *temp = json_object_array_get_idx(characters, i); blob - 3f679ef6b1466247cf00281637bf32e76a78c02e blob + d4651c6e7888d7d46514fe94044947ae93e77b51 --- fight.c +++ fight.c @@ -440,7 +440,12 @@ save_fight() json_object_object_add(root, "fight", items); } else { /* Get existing character array from JSON */ - items = json_object_object_get(root, "fight"); + if (!json_object_object_get_ex(root, "fight", &items)) { + log_debug("Cannot find a [fight] array in %s. Create one\n", path); + items = json_object_new_array(); + json_object_object_add(root, "fight", items); + } + temp_n = json_object_array_length(items); for (i = 0; i < temp_n; i++) { json_object *temp = json_object_array_get_idx(items, i); @@ -478,7 +483,12 @@ delete_fight(int id) return; } - json_object *fight = json_object_object_get(root, "fight"); + json_object *fight; + if (!json_object_object_get_ex(root, "fight", &fight)) { + log_debug("Cannot find a [fight] array in %s\n", path); + return; + } + temp_n = json_object_array_length(fight); for (i = 0; i < temp_n; i++) { json_object *temp = json_object_array_get_idx(fight, i); @@ -516,7 +526,12 @@ load_fight(int id) return; } - json_object *fight = json_object_object_get(root, "fight"); + json_object *fight; + if (!json_object_object_get_ex(root, "fight", &fight)) { + log_debug("Cannot find a [fight] array in %s\n", path); + return; + } + temp_n = json_object_array_length(fight); for (i=0; i < temp_n; i++) { json_object *temp = json_object_array_get_idx(fight, i); blob - af6199d19ae33dd0068b82bf2d23da62a075f688 blob + 76783c44f134361d41992aae4af524c005fe5c3d --- journey.c +++ journey.c @@ -221,7 +221,12 @@ save_journey() json_object_object_add(root, "journey", items); } else { /* Get existing character array from JSON */ - items = json_object_object_get(root, "journey"); + if (!json_object_object_get_ex(root, "journey", &items)) { + log_debug("Cannot find a [journey] array in %s\n", path); + items = json_object_new_array(); + json_object_object_add(root, "journey", items); + } + temp_n = json_object_array_length(items); for (i = 0; i < temp_n; i++) { json_object *temp = json_object_array_get_idx(items, i); @@ -259,7 +264,12 @@ delete_journey(int id) return; } - json_object *journey = json_object_object_get(root, "journey"); + json_object *journey; + if (!json_object_object_get_ex(root, "journey", &journey)) { + log_debug("Cannot find a [journey] array in %s\n", path); + return; + } + temp_n = json_object_array_length(journey); for (i = 0; i < temp_n; i++) { json_object *temp = json_object_array_get_idx(journey, i); @@ -297,7 +307,12 @@ load_journey(int id) return; } - json_object *journey = json_object_object_get(root, "journey"); + json_object *journey; + if (!json_object_object_get_ex(root, "journey", &journey)) { + log_debug("Cannot find a [journey] array in %s\n", path); + return; + } + temp_n = json_object_array_length(journey); for (i=0; i < temp_n; i++) { json_object *temp = json_object_array_get_idx(journey, i);