Skip to content

Commit 4ca1878

Browse files
ttaylorrgitster
authored andcommitted
pack-bitmap: fix pseudo-merge lookup for shared commits
When a commit appears in more than one pseudo-merge group, its entry in the commit lookup table has the high bit set in its offset field, indicating that the offset points to an "extended" table containing the set of pseudo-merges for that commit. There are three bugs in this path: * The `next_ext` offset in `write_pseudo_merges()` undercounts the per-entry size of the lookup table (8 vs. 12 bytes). * `nth_pseudo_merge_ext()` calls `read_pseudo_merge_commit_at()` on a pseudo-merge bitmap offset, misinterpreting it as a 12-byte commit table entry. * The error check after `pseudo_merge_ext_at()` in `apply_pseudo_merges_for_commit()` tests `< -1` instead of `< 0`, silently swallowing errors from `error()`. The first bug is on the write side: each commit lookup entry contains a 4- and 8-byte unsigned value for a total of 12 bytes, but the calculation assumes that the entry only contains 8 bytes of data. This makes `next_ext` too small, so the extended-table offsets that get written point into the middle of the non-extended lookup table rather than past it. The reader then interprets non-extended lookup data as extended entries, producing garbage. The second bug is on the read side and is independently fatal: even with a correctly positioned extended table, `nth_pseudo_merge_ext()` feeds the offset it reads (which points at pseudo-merge bitmap data) to `read_pseudo_merge_commit_at()`. That function tries to parse 12 bytes as a `pseudo_merge_commit` struct, clobbering `merge->pseudo_merge_ofs` with whatever happens to be at that location. The caller only needs `pseudo_merge_ofs`, so the fix is to store the offset directly rather than re-parsing a commit table entry. The `commit_pos` field is left untouched, retaining the value that `find_pseudo_merge()` set earlier. The third bug is latent. With the first two fixes applied, the extended table is correctly written and read, so `pseudo_merge_ext_at()` does not fail during normal operation. The `< -1` vs `< 0` distinction only matters when the bitmap file is corrupt or truncated, in which case the error would be silently ignored and the code would proceed with uninitialized data. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 9ca8305 commit 4ca1878

3 files changed

Lines changed: 4 additions & 4 deletions

File tree

pack-bitmap-write.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ static void write_pseudo_merges(struct bitmap_writer *writer,
877877

878878
next_ext = st_add(hashfile_total(f),
879879
st_mult(kh_size(writer->pseudo_merge_commits),
880-
sizeof(uint64_t)));
880+
sizeof(uint32_t) + sizeof(uint64_t)));
881881

882882
table_start = hashfile_total(f);
883883

pseudo-merge.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ static int nth_pseudo_merge_ext(const struct pseudo_merge_map *pm,
600600
return error(_("out-of-bounds read: (%"PRIuMAX" >= %"PRIuMAX")"),
601601
(uintmax_t)ofs, (uintmax_t)pm->map_size);
602602

603-
read_pseudo_merge_commit_at(merge, pm->map + ofs);
603+
merge->pseudo_merge_ofs = ofs;
604604

605605
return 0;
606606
}
@@ -671,7 +671,7 @@ int apply_pseudo_merges_for_commit(const struct pseudo_merge_map *pm,
671671
off_t ofs = merge_commit.pseudo_merge_ofs & ~((uint64_t)1<<63);
672672
uint32_t i;
673673

674-
if (pseudo_merge_ext_at(pm, &ext, ofs) < -1) {
674+
if (pseudo_merge_ext_at(pm, &ext, ofs) < 0) {
675675
warning(_("could not read extended pseudo-merge table "
676676
"for commit %s"),
677677
oid_to_hex(&commit->object.oid));

t/t5333-pseudo-merge-bitmaps.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ test_expect_success 'apply pseudo-merges from multiple groups during fill-in' '
549549
)
550550
'
551551

552-
test_expect_failure 'apply pseudo-merges with overlapping groups during fill-in' '
552+
test_expect_success 'apply pseudo-merges with overlapping groups during fill-in' '
553553
test_when_finished "rm -fr pseudo-merge-fill-in-overlap" &&
554554
git init pseudo-merge-fill-in-overlap &&
555555
(

0 commit comments

Comments
 (0)