Skip to content

Commit 25e5ceb

Browse files
peffgitster
authored andcommitted
pseudo-merge: fix disk reads from find_pseudo_merge()
The goal of this commit was to fix a const warning when compiling with new versions of glibc, but ended up untangling a much deeper problem. The find_pseudo_merge() function does a bsearch() on the "commits" pointer of a pseudo_merge_map. This pointer ultimately comes from memory mapped from the on-disk bitmap file, and is thus not writable. The "commits" array is correctly marked const, but the result from bsearch() is returned directly as a non-const pseudo_merge_commit struct. Since new versions of glibc annotate bsearch() in a way that detects the implicit loss of const, the compiler now warns. My first instinct was that we should be returning a const struct. That requires apply_pseudo_merges_for_commit() to mark its local pointer as const. But that doesn't work! If the offset field has the high-bit set, we look it up in the extended table via nth_pseudo_merge_ext(). And that function then feeds our const struct to read_pseudo_merge_commit_at(), which writes into it by byte-swapping from the on-disk mmap. But I think this points to a larger problem with find_pseudo_merge(). It is not just that the return value is missing const, but it is missing that byte-swapping! And we know that byte-swapping is needed here, because the comparator we use for bsearch() also calls our read_pseudo_merge_commit_at() helper. So I think the interface is all wrong here. We should not be returning a pointer to a struct which was cast from on-disk data. We should be filling in a caller-provided struct using the bytes we found, byte-swapping the values. That of course raises the dual question: how did this ever work, and does it work now? The answer to the first part is: this code does not seem to be triggered in the test suite at all. If we insert a BUG("foo") call into apply_pseudo_merges_for_commit(), it never triggers. So I think there is something wrong or missing from the test setup, and this bears further investigation. Sadly the answer to the second part ("does it work now") is still "no idea". I _think_ this takes us in a positive direction, but my goal here is mainly to quiet the compiler warning. Further bug-hunting on this experimental feature can be done separately. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent bc4fd55 commit 25e5ceb

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

pseudo-merge.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -638,31 +638,37 @@ static int pseudo_merge_commit_cmp(const void *va, const void *vb)
638638
return 0;
639639
}
640640

641-
static struct pseudo_merge_commit *find_pseudo_merge(const struct pseudo_merge_map *pm,
642-
uint32_t pos)
641+
static int find_pseudo_merge(const struct pseudo_merge_map *pm, uint32_t pos,
642+
struct pseudo_merge_commit *out)
643643
{
644+
const unsigned char *at;
645+
644646
if (!pm->commits_nr)
645-
return NULL;
647+
return 0;
646648

647-
return bsearch(&pos, pm->commits, pm->commits_nr,
648-
PSEUDO_MERGE_COMMIT_RAWSZ, pseudo_merge_commit_cmp);
649+
at = bsearch(&pos, pm->commits, pm->commits_nr,
650+
PSEUDO_MERGE_COMMIT_RAWSZ, pseudo_merge_commit_cmp);
651+
if (!at)
652+
return 0;
653+
654+
read_pseudo_merge_commit_at(out, at);
655+
return 1;
649656
}
650657

651658
int apply_pseudo_merges_for_commit(const struct pseudo_merge_map *pm,
652659
struct bitmap *result,
653660
struct commit *commit, uint32_t commit_pos)
654661
{
655662
struct pseudo_merge *merge;
656-
struct pseudo_merge_commit *merge_commit;
663+
struct pseudo_merge_commit merge_commit;
657664
int ret = 0;
658665

659-
merge_commit = find_pseudo_merge(pm, commit_pos);
660-
if (!merge_commit)
666+
if (!find_pseudo_merge(pm, commit_pos, &merge_commit))
661667
return 0;
662668

663-
if (merge_commit->pseudo_merge_ofs & ((uint64_t)1<<63)) {
669+
if (merge_commit.pseudo_merge_ofs & ((uint64_t)1<<63)) {
664670
struct pseudo_merge_commit_ext ext = { 0 };
665-
off_t ofs = merge_commit->pseudo_merge_ofs & ~((uint64_t)1<<63);
671+
off_t ofs = merge_commit.pseudo_merge_ofs & ~((uint64_t)1<<63);
666672
uint32_t i;
667673

668674
if (pseudo_merge_ext_at(pm, &ext, ofs) < -1) {
@@ -673,11 +679,11 @@ int apply_pseudo_merges_for_commit(const struct pseudo_merge_map *pm,
673679
}
674680

675681
for (i = 0; i < ext.nr; i++) {
676-
if (nth_pseudo_merge_ext(pm, &ext, merge_commit, i) < 0)
682+
if (nth_pseudo_merge_ext(pm, &ext, &merge_commit, i) < 0)
677683
return ret;
678684

679685
merge = pseudo_merge_at(pm, &commit->object.oid,
680-
merge_commit->pseudo_merge_ofs);
686+
merge_commit.pseudo_merge_ofs);
681687

682688
if (!merge)
683689
return ret;
@@ -687,7 +693,7 @@ int apply_pseudo_merges_for_commit(const struct pseudo_merge_map *pm,
687693
}
688694
} else {
689695
merge = pseudo_merge_at(pm, &commit->object.oid,
690-
merge_commit->pseudo_merge_ofs);
696+
merge_commit.pseudo_merge_ofs);
691697

692698
if (!merge)
693699
return ret;

0 commit comments

Comments
 (0)