midx: close multi-pack-index on repack

When repacking, we may remove pack-files. This invalidates the multi-pack-index (if it exists). Previously, we removed the multi-pack-index file before removing any pack-file. In some cases, the repack command may load the multi-pack-index into memory. This may lead to later in-memory references to the non-existent pack- files. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Oct 12, 2018 at 10:34 UTC 1dcd9f2043a38f0c9684d47c71b9e383942660ac
3 files changed +16 -6
builtin/repack.c
+1 -2
@@ -431,8 +431,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
431 char *fname, *fname_old;
432
433 if (!midx_cleared) {
434 - /* if we move a packfile, it will invalidated the midx */
435 - clear_midx_file(get_object_directory());
434 + clear_midx_file(the_repository);
435 midx_cleared = 1;
436 }
437
midx.c
+12 -3
@@ -180,9 +180,13 @@ cleanup_fail:
180 return NULL;
181 }
182
183 -static void close_midx(struct multi_pack_index *m)
183 +void close_midx(struct multi_pack_index *m)
184 {
185 uint32_t i;
186 +
187 + if (!m)
188 + return;
189 +
190 munmap((unsigned char *)m->data, m->data_len);
191 close(m->fd);
192 m->fd = -1;
@@ -917,9 +921,14 @@ cleanup:
921 return 0;
922 }
923
920 -void clear_midx_file(const char *object_dir)
924 +void clear_midx_file(struct repository *r)
925 {
922 - char *midx = get_midx_filename(object_dir);
926 + char *midx = get_midx_filename(r->objects->objectdir);
927 +
928 + if (r->objects && r->objects->multi_pack_index) {
929 + close_midx(r->objects->multi_pack_index);
930 + r->objects->multi_pack_index = NULL;
931 + }
932
933 if (remove_path(midx)) {
934 UNLEAK(midx);
midx.h
+3 -1
@@ -42,6 +42,8 @@ int midx_contains_pack(struct multi_pack_index *m, const char *idx_name);
42 int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, int local);
43
44 int write_midx_file(const char *object_dir);
45 -void clear_midx_file(const char *object_dir);
45 +void clear_midx_file(struct repository *r);
46 +
47 +void close_midx(struct multi_pack_index *m);
48
49 #endif