]> git.ipfire.org Git - thirdparty/git.git/commitdiff
commit-graph: fix truncated generation numbers
authorPatrick Steinhardt <ps@pks.im>
Mon, 27 Mar 2023 08:08:25 +0000 (10:08 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 27 Mar 2023 17:52:06 +0000 (10:52 -0700)
In 80c928d947 (commit-graph: simplify compute_generation_numbers(),
2023-03-20), the code to compute generation numbers was simplified to
use the same infrastructure as is used to compute topological levels.
This refactoring introduced a bug where the generation numbers are
truncated when they exceed UINT32_MAX because we explicitly cast the
computed generation number to `uint32_t`. This is not required though:
both the computed value and the field of `struct commit_graph_data` are
of the same type `timestamp_t` already, so casting to `uint32_t` will
cause truncation.

This cast can cause us to miscompute generation data overflows:

    1. Given a commit with no parents and committer date
       `UINT32_MAX + 1`.

    2. We compute its generation number as `UINT32_MAX + 1`, but
       truncate it to `1`.

    3. We calculate the generation offset via `$generation - $date`,
       which is thus `1 - (UINT32_MAX + 1)`. The computation underflows
       and we thus end up with an offset that is bigger than the maximum
       allowed offset.

As a result, we'd be writing generation data overflow information into
the commit-graph that is bogus and ultimately not even required.

Fix this bug by removing the needless cast.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
commit-graph.c
t/t5328-commit-graph-64bit-time.sh

index 172e679db1977f64b5bf2253a4a3ec7dc21b9dd8..b96509354ed7afa5a378f8b74cb758063653ab54 100644 (file)
@@ -1565,7 +1565,7 @@ static timestamp_t get_generation_from_graph_data(struct commit *c, void *data)
 static void set_generation_v2(struct commit *c, timestamp_t t, void *data)
 {
        struct commit_graph_data *g = commit_graph_data_at(c);
-       g->generation = (uint32_t)t;
+       g->generation = t;
 }
 
 static void compute_generation_numbers(struct write_commit_graph_context *ctx)
index 093f0c067af6801e3334152fe65b7c4495830d30..57e4d9c6998c2b690881984f5809fba6525bd4cc 100755 (executable)
@@ -63,4 +63,13 @@ test_expect_success 'set up and verify repo with generation data overflow chunk'
 
 graph_git_behavior 'overflow 2' repo left right
 
+test_expect_success 'single commit with generation data exceeding UINT32_MAX' '
+       git init repo-uint32-max &&
+       cd repo-uint32-max &&
+       test_commit --date "@4294967297 +0000" 1 &&
+       git commit-graph write --reachable &&
+       graph_read_expect 1 "generation_data" &&
+       git commit-graph verify
+'
+
 test_done