Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Commit

Permalink
idevicepair: Plug some memory leaks and fix option parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nikias committed Jun 8, 2020
1 parent 2a20540 commit b991ffe
Showing 1 changed file with 39 additions and 48 deletions.
87 changes: 39 additions & 48 deletions tools/idevicepair.c
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = {
Expand All @@ -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) {
Expand All @@ -109,49 +121,35 @@ 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':
idevice_set_debug_level(1);
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];
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}

0 comments on commit b991ffe

Please sign in to comment.