diff options
| author | 2020-06-08 19:51:26 +0200 | |
|---|---|---|
| committer | 2020-06-08 19:51:26 +0200 | |
| commit | b991ffefc37c1695d282588f2f2d7e2a578faabf (patch) | |
| tree | f0393195e00621eb1e9ce519ed9baf4a6db14017 | |
| parent | 2a20540995cb81f23d741c6962342ab14ab5233f (diff) | |
| download | libimobiledevice-b991ffefc37c1695d282588f2f2d7e2a578faabf.tar.gz libimobiledevice-b991ffefc37c1695d282588f2f2d7e2a578faabf.tar.bz2 | |
idevicepair: Plug some memory leaks and fix option parsing
| -rw-r--r-- | tools/idevicepair.c | 87 |
1 files changed, 39 insertions, 48 deletions
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) | |||
| 89 | printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n"); | 89 | printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n"); |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | static void parse_opts(int argc, char **argv) | 92 | int main(int argc, char **argv) |
| 93 | { | 93 | { |
| 94 | int c = 0; | 94 | int c = 0; |
| 95 | static struct option longopts[] = { | 95 | static struct option longopts[] = { |
| @@ -99,6 +99,18 @@ static void parse_opts(int argc, char **argv) | |||
| 99 | { "version", no_argument, NULL, 'v' }, | 99 | { "version", no_argument, NULL, 'v' }, |
| 100 | { NULL, 0, NULL, 0} | 100 | { NULL, 0, NULL, 0} |
| 101 | }; | 101 | }; |
| 102 | lockdownd_client_t client = NULL; | ||
| 103 | idevice_t device = NULL; | ||
| 104 | idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; | ||
| 105 | lockdownd_error_t lerr; | ||
| 106 | int result; | ||
| 107 | |||
| 108 | char *type = NULL; | ||
| 109 | char *cmd; | ||
| 110 | typedef enum { | ||
| 111 | OP_NONE = 0, OP_PAIR, OP_VALIDATE, OP_UNPAIR, OP_LIST, OP_HOSTID, OP_SYSTEMBUID | ||
| 112 | } op_t; | ||
| 113 | op_t op = OP_NONE; | ||
| 102 | 114 | ||
| 103 | while ((c = getopt_long(argc, argv, "hu:dv", longopts, NULL)) != -1) { | 115 | while ((c = getopt_long(argc, argv, "hu:dv", longopts, NULL)) != -1) { |
| 104 | switch (c) { | 116 | switch (c) { |
| @@ -109,10 +121,10 @@ static void parse_opts(int argc, char **argv) | |||
| 109 | if (!*optarg) { | 121 | if (!*optarg) { |
| 110 | fprintf(stderr, "ERROR: UDID must not be empty!\n"); | 122 | fprintf(stderr, "ERROR: UDID must not be empty!\n"); |
| 111 | print_usage(argc, argv); | 123 | print_usage(argc, argv); |
| 112 | exit(2); | 124 | result = EXIT_FAILURE; |
| 125 | goto leave; | ||
| 113 | } | 126 | } |
| 114 | if (udid) | 127 | free(udid); |
| 115 | free(udid); | ||
| 116 | udid = strdup(optarg); | 128 | udid = strdup(optarg); |
| 117 | break; | 129 | break; |
| 118 | case 'd': | 130 | case 'd': |
| @@ -120,38 +132,24 @@ static void parse_opts(int argc, char **argv) | |||
| 120 | break; | 132 | break; |
| 121 | case 'v': | 133 | case 'v': |
| 122 | printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); | 134 | printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION); |
| 123 | exit(EXIT_SUCCESS); | 135 | result = EXIT_SUCCESS; |
| 136 | goto leave; | ||
| 124 | default: | 137 | default: |
| 125 | print_usage(argc, argv); | 138 | print_usage(argc, argv); |
| 126 | exit(EXIT_SUCCESS); | 139 | result = EXIT_FAILURE; |
| 140 | goto leave; | ||
| 127 | } | 141 | } |
| 128 | } | 142 | } |
| 129 | } | ||
| 130 | |||
| 131 | int main(int argc, char **argv) | ||
| 132 | { | ||
| 133 | lockdownd_client_t client = NULL; | ||
| 134 | idevice_t device = NULL; | ||
| 135 | idevice_error_t ret = IDEVICE_E_UNKNOWN_ERROR; | ||
| 136 | lockdownd_error_t lerr; | ||
| 137 | int result; | ||
| 138 | |||
| 139 | char *type = NULL; | ||
| 140 | char *cmd; | ||
| 141 | typedef enum { | ||
| 142 | OP_NONE = 0, OP_PAIR, OP_VALIDATE, OP_UNPAIR, OP_LIST, OP_HOSTID, OP_SYSTEMBUID | ||
| 143 | } op_t; | ||
| 144 | op_t op = OP_NONE; | ||
| 145 | 143 | ||
| 146 | #ifndef WIN32 | 144 | #ifndef WIN32 |
| 147 | signal(SIGPIPE, SIG_IGN); | 145 | signal(SIGPIPE, SIG_IGN); |
| 148 | #endif | 146 | #endif |
| 149 | parse_opts(argc, argv); | ||
| 150 | 147 | ||
| 151 | if ((argc - optind) < 1) { | 148 | if ((argc - optind) < 1) { |
| 152 | printf("ERROR: You need to specify a COMMAND!\n"); | 149 | printf("ERROR: You need to specify a COMMAND!\n"); |
| 153 | print_usage(argc, argv); | 150 | print_usage(argc, argv); |
| 154 | exit(EXIT_FAILURE); | 151 | result = EXIT_FAILURE; |
| 152 | goto leave; | ||
| 155 | } | 153 | } |
| 156 | 154 | ||
| 157 | cmd = (argv+optind)[0]; | 155 | cmd = (argv+optind)[0]; |
| @@ -180,10 +178,10 @@ int main(int argc, char **argv) | |||
| 180 | 178 | ||
| 181 | printf("%s\n", systembuid); | 179 | printf("%s\n", systembuid); |
| 182 | 180 | ||
| 183 | if (systembuid) | 181 | free(systembuid); |
| 184 | free(systembuid); | ||
| 185 | 182 | ||
| 186 | return EXIT_SUCCESS; | 183 | result = EXIT_SUCCESS; |
| 184 | goto leave; | ||
| 187 | } | 185 | } |
| 188 | 186 | ||
| 189 | if (op == OP_LIST) { | 187 | if (op == OP_LIST) { |
| @@ -195,11 +193,9 @@ int main(int argc, char **argv) | |||
| 195 | printf("%s\n", udids[i]); | 193 | printf("%s\n", udids[i]); |
| 196 | free(udids[i]); | 194 | free(udids[i]); |
| 197 | } | 195 | } |
| 198 | if (udids) | 196 | free(udids); |
| 199 | free(udids); | 197 | result = EXIT_SUCCESS; |
| 200 | if (udid) | 198 | goto leave; |
| 201 | free(udid); | ||
| 202 | return EXIT_SUCCESS; | ||
| 203 | } | 199 | } |
| 204 | 200 | ||
| 205 | ret = idevice_new(&device, udid); | 201 | ret = idevice_new(&device, udid); |
| @@ -209,8 +205,8 @@ int main(int argc, char **argv) | |||
| 209 | } else { | 205 | } else { |
| 210 | printf("No device found.\n"); | 206 | printf("No device found.\n"); |
| 211 | } | 207 | } |
| 212 | free(udid); | 208 | result = EXIT_FAILURE; |
| 213 | return EXIT_FAILURE; | 209 | goto leave; |
| 214 | } | 210 | } |
| 215 | if (!udid) { | 211 | if (!udid) { |
| 216 | ret = idevice_get_udid(device, &udid); | 212 | ret = idevice_get_udid(device, &udid); |
| @@ -230,20 +226,18 @@ int main(int argc, char **argv) | |||
| 230 | 226 | ||
| 231 | printf("%s\n", hostid); | 227 | printf("%s\n", hostid); |
| 232 | 228 | ||
| 233 | if (hostid) | 229 | free(hostid); |
| 234 | free(hostid); | 230 | plist_free(pair_record); |
| 235 | |||
| 236 | if (pair_record) | ||
| 237 | plist_free(pair_record); | ||
| 238 | 231 | ||
| 239 | return EXIT_SUCCESS; | 232 | result = EXIT_SUCCESS; |
| 233 | goto leave; | ||
| 240 | } | 234 | } |
| 241 | 235 | ||
| 242 | lerr = lockdownd_client_new(device, &client, TOOL_NAME); | 236 | lerr = lockdownd_client_new(device, &client, TOOL_NAME); |
| 243 | if (lerr != LOCKDOWN_E_SUCCESS) { | 237 | if (lerr != LOCKDOWN_E_SUCCESS) { |
| 244 | idevice_free(device); | ||
| 245 | printf("ERROR: Could not connect to lockdownd, error code %d\n", lerr); | 238 | printf("ERROR: Could not connect to lockdownd, error code %d\n", lerr); |
| 246 | return EXIT_FAILURE; | 239 | result = EXIT_FAILURE; |
| 240 | goto leave; | ||
| 247 | } | 241 | } |
| 248 | 242 | ||
| 249 | result = EXIT_SUCCESS; | 243 | result = EXIT_SUCCESS; |
| @@ -257,9 +251,7 @@ int main(int argc, char **argv) | |||
| 257 | if (strcmp("com.apple.mobile.lockdown", type)) { | 251 | if (strcmp("com.apple.mobile.lockdown", type)) { |
| 258 | printf("WARNING: QueryType request returned '%s'\n", type); | 252 | printf("WARNING: QueryType request returned '%s'\n", type); |
| 259 | } | 253 | } |
| 260 | if (type) { | 254 | free(type); |
| 261 | free(type); | ||
| 262 | } | ||
| 263 | } | 255 | } |
| 264 | 256 | ||
| 265 | switch(op) { | 257 | switch(op) { |
| @@ -300,9 +292,8 @@ int main(int argc, char **argv) | |||
| 300 | leave: | 292 | leave: |
| 301 | lockdownd_client_free(client); | 293 | lockdownd_client_free(client); |
| 302 | idevice_free(device); | 294 | idevice_free(device); |
| 303 | if (udid) { | 295 | free(udid); |
| 304 | free(udid); | 296 | |
| 305 | } | ||
| 306 | return result; | 297 | return result; |
| 307 | } | 298 | } |
| 308 | 299 | ||
