Skip to content

Commit 5e915f3

Browse files
peffgitster
authored andcommitted
fast-import: avoid running end_packfile recursively
When an import has finished, we run end_packfile() to finalize the data and move the packfile into place. If this process fails, we call die() and end up in our die_nicely() handler. Which unfortunately includes running end_packfile to save any progress we made. We enter the function again, and start operating on the pack_data struct while it is in an inconsistent state, leading to a segfault. One way to trigger this is to simply start two identical fast-imports at the same time. They will both create the same packfiles, which will then try to create identically named ".keep" files. One will win the race, and the other will die(), and end up with the segfault. Since 3c078b9, we already reset the pack_data pointer to NULL at the end of end_packfile. That covers the case of us calling die() right after end_packfile, before we have reinitialized the pack_data pointer. This new problem is quite similar, except that we are worried about calling die() _during_ end_packfile, not right after. Ideally we would simply set pack_data to NULL as soon as we enter the function, and operate on a copy of the pointer. Unfortunately, it is not so easy. pack_data is a global, and end_packfile calls into other functions which operate on the global directly. We would have to teach each of these to take an argument, and there is no guarantee that we would catch all of the spots. Instead, we can simply use a static flag to avoid recursively entering the function. This is a little less elegant, but it's short and fool-proof. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3c84ac8 commit 5e915f3

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

fast-import.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,9 +946,12 @@ static void unkeep_all_packs(void)
946946

947947
static void end_packfile(void)
948948
{
949-
if (!pack_data)
949+
static int running;
950+
951+
if (running || !pack_data)
950952
return;
951953

954+
running = 1;
952955
clear_delta_base_cache();
953956
if (object_count) {
954957
struct packed_git *new_p;
@@ -998,6 +1001,7 @@ static void end_packfile(void)
9981001
}
9991002
free(pack_data);
10001003
pack_data = NULL;
1004+
running = 0;
10011005

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

0 commit comments

Comments
 (0)