diff options
author | BALATON Zoltan | 2015-05-05 00:58:02 +0200 |
---|---|---|
committer | BALATON Zoltan | 2015-05-05 00:58:02 +0200 |
commit | 245bc9b6510a8900c69c2fd543434bae1bc6ca6a (patch) | |
tree | aede07c0073fc4bf0e178147ac01dec9fc5aa234 /src/restore.c | |
parent | 7fdcedea3aac19ef8a2a660cfe809bd8c0f5f57f (diff) | |
download | idevicerestore-245bc9b6510a8900c69c2fd543434bae1bc6ca6a.tar.gz idevicerestore-245bc9b6510a8900c69c2fd543434bae1bc6ca6a.tar.bz2 |
Clean up free() usage and plug some potential memory leaks
Diffstat (limited to 'src/restore.c')
-rw-r--r-- | src/restore.c | 131 |
1 files changed, 61 insertions, 70 deletions
diff --git a/src/restore.c b/src/restore.c index 9775562..79e1828 100644 --- a/src/restore.c +++ b/src/restore.c @@ -80,23 +80,21 @@ int restore_client_new(struct idevicerestore_client_t* client) { } void restore_client_free(struct idevicerestore_client_t* client) { - if (client) { - if(client->restore) { - if(client->restore->client) { - restored_client_free(client->restore->client); - client->restore->client = NULL; - } - if(client->restore->device) { - idevice_free(client->restore->device); - client->restore->device = NULL; - } - if(client->restore->bbtss) { - plist_free(client->restore->bbtss); - client->restore->bbtss = NULL; - } - free(client->restore); - client->restore = NULL; + if (client && client->restore) { + if(client->restore->client) { + restored_client_free(client->restore->client); + client->restore->client = NULL; } + if(client->restore->device) { + idevice_free(client->restore->device); + client->restore->device = NULL; + } + if(client->restore->bbtss) { + plist_free(client->restore->bbtss); + client->restore->bbtss = NULL; + } + free(client->restore); + client->restore = NULL; } } @@ -781,8 +779,7 @@ int restore_send_kernelcache(restored_client_t restore, struct idevicerestore_cl if (!path) { if (build_identity_get_component_path(build_identity, "KernelCache", &path) < 0) { error("ERROR: Unable to find kernelcache path\n"); - if (path) - free(path); + free(path); return -1; } } @@ -790,37 +787,36 @@ int restore_send_kernelcache(restored_client_t restore, struct idevicerestore_cl const char* component = "KernelCache"; unsigned char* component_data = NULL; unsigned int component_size = 0; - - if (extract_component(client->ipsw, path, &component_data, &component_size) < 0) { + int ret = extract_component(client->ipsw, path, &component_data, &component_size); + free(path); + path = NULL; + if (ret < 0) { error("ERROR: Unable to extract component: %s\n", component); - free(path); return -1; } - if (personalize_component(component, component_data, component_size, client->tss, &data, &size) < 0) { + ret = personalize_component(component, component_data, component_size, client->tss, &data, &size); + free(component_data); + component_data = NULL; + if (ret < 0) { error("ERROR: Unable to get personalized component: %s\n", component); - free(component_data); - free(path); return -1; } - free(component_data); - component_data = NULL; dict = plist_new_dict(); blob = plist_new_data((char*)data, size); plist_dict_set_item(dict, "KernelCacheFile", blob); + free(data); info("Sending KernelCache now...\n"); restore_error = restored_send(restore, dict); + plist_free(dict); if (restore_error != RESTORE_E_SUCCESS) { error("ERROR: Unable to send kernelcache data\n"); - plist_free(dict); return -1; } info("Done sending KernelCache\n"); - plist_free(dict); - free(data); return 0; } @@ -841,7 +837,6 @@ int restore_send_nor(restored_client_t restore, struct idevicerestore_client_t* unsigned int nor_size = 0; unsigned char* nor_data = NULL; plist_t norimage_array = NULL; - restored_error_t ret = RESTORE_E_SUCCESS; info("About to send NORData...\n"); @@ -853,8 +848,7 @@ int restore_send_nor(restored_client_t restore, struct idevicerestore_client_t* if (llb_path == NULL) { if (build_identity_get_component_path(build_identity, "LLB", &llb_path) < 0) { error("ERROR: Unable to get component path for LLB\n"); - if (llb_path) - free(llb_path); + free(llb_path); return -1; } } @@ -883,27 +877,25 @@ int restore_send_nor(restored_client_t restore, struct idevicerestore_client_t* const char* component = "LLB"; unsigned char* component_data = NULL; unsigned int component_size = 0; - - if (extract_component(client->ipsw, llb_path, &component_data, &component_size) < 0) { + int ret = extract_component(client->ipsw, llb_path, &component_data, &component_size); + free(llb_path); + if (ret < 0) { error("ERROR: Unable to extract component: %s\n", component); - free(llb_path); return -1; } - if (personalize_component(component, component_data, component_size, client->tss, &llb_data, &llb_size) < 0) { - error("ERROR: Unable to get personalized component: %s\n", component); - free(component_data); - free(llb_path); - return -1; - } + ret = personalize_component(component, component_data, component_size, client->tss, &llb_data, &llb_size); free(component_data); component_data = NULL; component_size = 0; + if (ret < 0) { + error("ERROR: Unable to get personalized component: %s\n", component); + return -1; + } dict = plist_new_dict(); plist_dict_set_item(dict, "LlbImageData", plist_new_data((char*)llb_data, (uint64_t) llb_size)); - if (llb_data) - free(llb_data); + free(llb_data); norimage_array = plist_new_array(); @@ -923,14 +915,12 @@ int restore_send_nor(restored_client_t restore, struct idevicerestore_client_t* if (extract_component(client->ipsw, firmware_filename, &component_data, &component_size) < 0) { error("ERROR: Unable to extract component: %s\n", component); - free(llb_path); return -1; } if (personalize_component(component, component_data, component_size, client->tss, &nor_data, &nor_size) < 0) { error("ERROR: Unable to get personalized component: %s\n", component); free(component_data); - free(llb_path); return -1; } free(component_data); @@ -954,23 +944,24 @@ int restore_send_nor(restored_client_t restore, struct idevicerestore_client_t* unsigned char* personalized_data = NULL; unsigned int personalized_size = 0; - if (!build_identity_has_component(build_identity, "RestoreSEP") && build_identity_get_component_path(build_identity, "RestoreSEP", &restore_sep_path) == 0) { + if (!build_identity_has_component(build_identity, "RestoreSEP") && + build_identity_get_component_path(build_identity, "RestoreSEP", &restore_sep_path) == 0) { component = "RestoreSEP"; - if (extract_component(client->ipsw, restore_sep_path, &component_data, &component_size) < 0) { + ret = extract_component(client->ipsw, restore_sep_path, &component_data, &component_size); + free(restore_sep_path); + if (ret < 0) { error("ERROR: Unable to extract component: %s\n", component); - free(restore_sep_path); return -1; } - if (personalize_component(component, component_data, component_size, client->tss, &personalized_data, &personalized_size) < 0) { - error("ERROR: Unable to get personalized component: %s\n", component); - free(component_data); - free(restore_sep_path); - return -1; - } + ret = personalize_component(component, component_data, component_size, client->tss, &personalized_data, &personalized_size); free(component_data); component_data = NULL; component_size = 0; + if (ret < 0) { + error("ERROR: Unable to get personalized component: %s\n", component); + return -1; + } plist_dict_set_item(dict, "RestoreSEPImageData", plist_new_data((char*)personalized_data, (uint64_t) personalized_size)); free(personalized_data); @@ -978,23 +969,24 @@ int restore_send_nor(restored_client_t restore, struct idevicerestore_client_t* personalized_size = 0; } - if (!build_identity_has_component(build_identity, "SEP") && build_identity_get_component_path(build_identity, "SEP", &sep_path) == 0) { + if (!build_identity_has_component(build_identity, "SEP") && + build_identity_get_component_path(build_identity, "SEP", &sep_path) == 0) { component = "SEP"; - if (extract_component(client->ipsw, sep_path, &component_data, &component_size) < 0) { + ret = extract_component(client->ipsw, sep_path, &component_data, &component_size); + free(sep_path); + if (ret < 0) { error("ERROR: Unable to extract component: %s\n", component); - free(sep_path); return -1; } - if (personalize_component(component, component_data, component_size, client->tss, &personalized_data, &personalized_size) < 0) { - error("ERROR: Unable to get personalized component: %s\n", component); - free(component_data); - free(sep_path); - return -1; - } + ret = personalize_component(component, component_data, component_size, client->tss, &personalized_data, &personalized_size); free(component_data); component_data = NULL; component_size = 0; + if (ret < 0) { + error("ERROR: Unable to get personalized component: %s\n", component); + return -1; + } plist_dict_set_item(dict, "SEPImageData", plist_new_data((char*)personalized_data, (uint64_t) personalized_size)); free(personalized_data); @@ -1006,8 +998,7 @@ int restore_send_nor(restored_client_t restore, struct idevicerestore_client_t* debug_plist(dict); info("Sending NORData now...\n"); - ret = restored_send(restore, dict); - if (ret != RESTORE_E_SUCCESS) { + if (restored_send(restore, dict) != RESTORE_E_SUCCESS) { error("ERROR: Unable to send NORImageData data\n"); plist_free(dict); return -1; @@ -1919,11 +1910,11 @@ int restore_device(struct idevicerestore_client_t* client, plist_t build_identit debug_plist(message); } - if (type) - free(type); - - plist_free(message); - message = NULL; + free(type); + if (message) { + plist_free(message); + message = NULL; + } } restore_client_free(client); |