From b991ffefc37c1695d282588f2f2d7e2a578faabf Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 8 Jun 2020 19:51:26 +0200 Subject: idevicepair: Plug some memory leaks and fix option parsing --- tools/idevicepair.c | 87 ++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 48 deletions(-) (limited to 'tools') diff --git a/tools/idevicepair.c b/tools/idevicepair.c index 68507f4..0dcd45f 100644 --- a/tools/idevicepair.c +++ b/tools/idevicepair.c @@ -89,7 +89,7 @@ static void print_usage(int argc, char **argv) printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n"); } -static void parse_opts(int argc, char **argv) +int main(int argc, char **argv) { int c = 0; static struct option longopts[] = { @@ -99,6 +99,18 @@ static void parse_opts(int argc, char **argv) { "version", no_argument, NULL, 'v' }, { NULL, 0, NULL, 0} }; + lockdownd_client_t client = NULL; + idevice_t device = NULL; + idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; + lockdownd_error_t lerr; + int result; + + char *type = NULL; + char *cmd; + typedef enum { + OP_NONE = 0, OP_PAIR, OP_VALIDATE, OP_UNPAIR, OP_LIST, OP_HOSTID, OP_SYSTEMBUID + } op_t; + op_t op = OP_NONE; while ((c = getopt_long(argc, argv, "hu:dv", longopts, NULL)) != -1) { switch (c) { @@ -109,10 +121,10 @@ static void parse_opts(int argc, char **argv) if (!*optarg) { fprintf(stderr, "ERROR: UDID must not be empty!\n"); print_usage(argc, argv); - exit(2); + result = EXIT_FAILURE; + goto leave; } - if (udid) - free(udid); + free(udid); udid = strdup(optarg); break; case 'd': @@ -120,38 +132,24 @@ static void parse_opts(int argc, char **argv) break; case 'v': printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); - exit(EXIT_SUCCESS); + result = EXIT_SUCCESS; + goto leave; default: print_usage(argc, argv); - exit(EXIT_SUCCESS); + result = EXIT_FAILURE; + goto leave; } } -} - -int main(int argc, char **argv) -{ - lockdownd_client_t client = NULL; - idevice_t device = NULL; - idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; - lockdownd_error_t lerr; - int result; - - char *type = NULL; - char *cmd; - typedef enum { - OP_NONE = 0, OP_PAIR, OP_VALIDATE, OP_UNPAIR, OP_LIST, OP_HOSTID, OP_SYSTEMBUID - } op_t; - op_t op = OP_NONE; #ifndef WIN32 signal(SIGPIPE, SIG_IGN); #endif - parse_opts(argc, argv); if ((argc - optind) < 1) { printf("ERROR: You need to specify a COMMAND!\n"); print_usage(argc, argv); - exit(EXIT_FAILURE); + result = EXIT_FAILURE; + goto leave; } cmd = (argv+optind)[0]; @@ -180,10 +178,10 @@ int main(int argc, char **argv) printf("%s\n", systembuid); - if (systembuid) - free(systembuid); + free(systembuid); - return EXIT_SUCCESS; + result = EXIT_SUCCESS; + goto leave; } if (op == OP_LIST) { @@ -195,11 +193,9 @@ int main(int argc, char **argv) printf("%s\n", udids[i]); free(udids[i]); } - if (udids) - free(udids); - if (udid) - free(udid); - return EXIT_SUCCESS; + free(udids); + result = EXIT_SUCCESS; + goto leave; } ret = idevice_new(&device, udid); @@ -209,8 +205,8 @@ int main(int argc, char **argv) } else { printf("No device found.\n"); } - free(udid); - return EXIT_FAILURE; + result = EXIT_FAILURE; + goto leave; } if (!udid) { ret = idevice_get_udid(device, &udid); @@ -230,20 +226,18 @@ int main(int argc, char **argv) printf("%s\n", hostid); - if (hostid) - free(hostid); - - if (pair_record) - plist_free(pair_record); + free(hostid); + plist_free(pair_record); - return EXIT_SUCCESS; + result = EXIT_SUCCESS; + goto leave; } lerr = lockdownd_client_new(device, &client, TOOL_NAME); if (lerr != LOCKDOWN_E_SUCCESS) { - idevice_free(device); printf("ERROR: Could not connect to lockdownd, error code %d\n", lerr); - return EXIT_FAILURE; + result = EXIT_FAILURE; + goto leave; } result = EXIT_SUCCESS; @@ -257,9 +251,7 @@ int main(int argc, char **argv) if (strcmp("com.apple.mobile.lockdown", type)) { printf("WARNING: QueryType request returned '%s'\n", type); } - if (type) { - free(type); - } + free(type); } switch(op) { @@ -300,9 +292,8 @@ int main(int argc, char **argv) leave: lockdownd_client_free(client); idevice_free(device); - if (udid) { - free(udid); - } + free(udid); + return result; } -- cgit v1.1-32-gdbae