Skip to content

Commit 1b4135b

Browse files
committed
Fix segfault when changing a library path on a fat binary.
1 parent f91c541 commit 1b4135b

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

macher.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,10 @@ static void slice_destroy(Slice slice)
186186
{
187187
for (int i = 0; i < slice->num_commands; i++) {
188188
mach_o_command *command = slice->commands + i;
189-
free(command->data);
190-
command->data = NULL;
189+
if (command->data) {
190+
free(command->data);
191+
command->data = NULL;
192+
}
191193
}
192194
if (slice->info) {
193195
NXFreeArchInfo(slice->info);
@@ -569,12 +571,13 @@ static void change_dylib_path(Slice slice, mach_o_command *command, char *path)
569571
struct dylib_command *dc = (struct dylib_command *) command->data;
570572
char *old_path = (char *) dc + dc->dylib.name.offset;
571573
struct dylib_command *new_command;
572-
int old_size = command->lc.cmdsize;
573-
int min_size = old_size + strlen(path) - strlen(old_path);
574-
int new_size = aligned_command_size(slice, min_size);
574+
unsigned int old_size = command->lc.cmdsize;
575+
unsigned int min_size = old_size + strlen(path) - strlen(old_path);
576+
unsigned int new_size = aligned_command_size(slice, min_size);
575577
int delta = new_size - old_size;
576578
char *tail;
577-
int tail_size = slice->command_space - command->position - old_size;
579+
unsigned int tail_size = slice->offset + slice->command_space -
580+
command->position - old_size;
578581
if (slice->command_block_size + delta > slice->command_space) {
579582
printf("There is not enough space in the file to change the id.\n");
580583
exit(1);
@@ -590,7 +593,7 @@ static void change_dylib_path(Slice slice, mach_o_command *command, char *path)
590593
new_command->cmdsize = new_size;
591594
strcpy((char *)new_command + new_command->dylib.name.offset, path);
592595
fwrite(command->data, new_size, 1, slice->mach_o_file);
593-
fwrite(tail, tail_size, 1, slice->mach_o_file);
596+
int answer = fwrite(tail, tail_size, 1, slice->mach_o_file);
594597
free(tail);
595598
for (int i = index; i < slice->num_commands; i++) {
596599
slice->commands[i].position += delta;
@@ -599,6 +602,7 @@ static void change_dylib_path(Slice slice, mach_o_command *command, char *path)
599602
slice->command_space -= delta;
600603
update_header(slice);
601604
free(command->data);
605+
command->data = NULL;
602606
}
603607

604608
static int edit_libpath(Slice slice, mach_o_command *command, char *libpath)

0 commit comments

Comments
 (0)