Skip to content

Commit 363485a

Browse files
ttaylorrgitster
authored andcommitted
midx-write.c: introduce midx_pack_perm() helper
The `ctx->pack_perm` array can be considered as a permutation between the original `pack_int_id` of some given pack to its position in the `ctx->info` array containing all packs. Today we can always index into this array with any known `pack_int_id`, since there is never a `pack_int_id` which is greater than or equal to the value `ctx->nr`. That is not necessarily the case with MIDX compaction. For example, suppose we have a MIDX chain with three layers, each containing three packs. The base of the MIDX chain will have packs with IDs 0, 1, and 2, the next layer 3, 4, and 5, and so on. If we are compacting the topmost two layers, we'll have input `pack_int_id` values between [3, 8], but `ctx->nr` will only be 6. In that example, if we want to know where the pack whose original `pack_int_id` value was, say, 7, we would compute `ctx->pack_perm[7]`, leading to an uninitialized read, since there are only 6 entries allocated in that array. To address this, there are a couple of options: - We could allocate enough entries in `ctx->pack_perm` to accommodate the largest `orig_pack_int_id` value. - Or, we could internally shift the input values by the number of packs in the base layer of the lower end of the MIDX compaction range. This patch prepare us to take the latter approach, since it does not allocate more memory than strictly necessary. (In our above example, the base of the lower end of the compaction range is the first MIDX layer (having three packs), so we would end up indexing `ctx->pack_perm[7-3]`, which is a valid read.) Note that this patch does not actually implement that approach yet, but merely performs a behavior-preserving refactoring which will make the change easier to carry out in the future. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 35364d7 commit 363485a

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

midx-write.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ struct write_midx_context {
114114
struct odb_source *source;
115115
};
116116

117+
static uint32_t midx_pack_perm(struct write_midx_context *ctx,
118+
uint32_t orig_pack_int_id)
119+
{
120+
return ctx->pack_perm[orig_pack_int_id];
121+
}
122+
117123
static int should_include_pack(const struct write_midx_context *ctx,
118124
const char *file_name)
119125
{
@@ -509,12 +515,12 @@ static int write_midx_object_offsets(struct hashfile *f,
509515
for (i = 0; i < ctx->entries_nr; i++) {
510516
struct pack_midx_entry *obj = list++;
511517

512-
if (ctx->pack_perm[obj->pack_int_id] == PACK_EXPIRED)
518+
if (midx_pack_perm(ctx, obj->pack_int_id) == PACK_EXPIRED)
513519
BUG("object %s is in an expired pack with int-id %d",
514520
oid_to_hex(&obj->oid),
515521
obj->pack_int_id);
516522

517-
hashwrite_be32(f, ctx->pack_perm[obj->pack_int_id]);
523+
hashwrite_be32(f, midx_pack_perm(ctx, obj->pack_int_id));
518524

519525
if (ctx->large_offsets_needed && obj->offset >> 31)
520526
hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
@@ -615,7 +621,7 @@ static uint32_t *midx_pack_order(struct write_midx_context *ctx)
615621
for (i = 0; i < ctx->entries_nr; i++) {
616622
struct pack_midx_entry *e = &ctx->entries[i];
617623
data[i].nr = i;
618-
data[i].pack = ctx->pack_perm[e->pack_int_id];
624+
data[i].pack = midx_pack_perm(ctx, e->pack_int_id);
619625
if (!e->preferred)
620626
data[i].pack |= (1U << 31);
621627
data[i].offset = e->offset;
@@ -625,7 +631,7 @@ static uint32_t *midx_pack_order(struct write_midx_context *ctx)
625631

626632
for (i = 0; i < ctx->entries_nr; i++) {
627633
struct pack_midx_entry *e = &ctx->entries[data[i].nr];
628-
struct pack_info *pack = &ctx->info[ctx->pack_perm[e->pack_int_id]];
634+
struct pack_info *pack = &ctx->info[midx_pack_perm(ctx, e->pack_int_id)];
629635
if (pack->bitmap_pos == BITMAP_POS_UNKNOWN)
630636
pack->bitmap_pos = i + base_objects;
631637
pack->bitmap_nr++;
@@ -686,7 +692,7 @@ static void prepare_midx_packing_data(struct packing_data *pdata,
686692
struct object_entry *to = packlist_alloc(pdata, &from->oid);
687693

688694
oe_set_in_pack(pdata, to,
689-
ctx->info[ctx->pack_perm[from->pack_int_id]].p);
695+
ctx->info[midx_pack_perm(ctx, from->pack_int_id)].p);
690696
}
691697

692698
trace2_region_leave("midx", "prepare_midx_packing_data", ctx->repo);
@@ -1285,7 +1291,7 @@ static int write_midx_internal(struct write_midx_opts *opts)
12851291
sizeof(*ctx.info),
12861292
idx_or_pack_name_cmp);
12871293
if (preferred) {
1288-
uint32_t perm = ctx.pack_perm[preferred->orig_pack_int_id];
1294+
uint32_t perm = midx_pack_perm(&ctx, preferred->orig_pack_int_id);
12891295
if (perm == PACK_EXPIRED)
12901296
warning(_("preferred pack '%s' is expired"),
12911297
opts->preferred_pack_name);

0 commit comments

Comments
 (0)