Skip to content

Commit 3c078b9

Browse files
peffgitster
authored andcommitted
fast-import: clean up pack_data pointer in end_packfile
We have a global pointer pack_data pointing to the current pack we have open. Inside end_packfile we have two new pointers, old_p and new_p. The latter points to pack_data, and the former points to the new "installed" version of the packfile we get when we hand the file off to the regular sha1_file machinery. When then free old_p. Presumably the extra old_p pointer was there so that we could overwrite pack_data with new_p and still free old_p, but we don't do that. We just leave pack_data pointing to bogus memory, and don't overwrite it until we call start_packfile again (if ever). This can cause problems for our die routine, which calls end_packfile to clean things up. If we die at the wrong moment, we can end up looking at invalid memory in pack_data left after the last end_packfile(). Instead, let's make sure we set pack_data to NULL after we free it, and make calling endfile() again with a NULL pack_data a noop (there is nothing to end). We can further make things less confusing by dropping old_p entirely, and moving new_p closer to its point of use. Signed-off-by: Jeff King <[email protected]> Reviewed-by: Ronnie Sahlberg <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 32f5660 commit 3c078b9

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

fast-import.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -945,10 +945,12 @@ static void unkeep_all_packs(void)
945945

946946
static void end_packfile(void)
947947
{
948-
struct packed_git *old_p = pack_data, *new_p;
948+
if (!pack_data)
949+
return;
949950

950951
clear_delta_base_cache();
951952
if (object_count) {
953+
struct packed_git *new_p;
952954
unsigned char cur_pack_sha1[20];
953955
char *idx_name;
954956
int i;
@@ -990,10 +992,11 @@ static void end_packfile(void)
990992
pack_id++;
991993
}
992994
else {
993-
close(old_p->pack_fd);
994-
unlink_or_warn(old_p->pack_name);
995+
close(pack_data->pack_fd);
996+
unlink_or_warn(pack_data->pack_name);
995997
}
996-
free(old_p);
998+
free(pack_data);
999+
pack_data = NULL;
9971000

9981001
/* We can't carry a delta across packfiles. */
9991002
strbuf_release(&last_blob.data);

0 commit comments

Comments
 (0)