Skip to content

Commit d3af1c1

Browse files
pks-tgitster
authored andcommitted
commit-graph: fix truncated generation numbers
In 80c928d (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cbfe360 commit d3af1c1

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

commit-graph.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1565,7 +1565,7 @@ static timestamp_t get_generation_from_graph_data(struct commit *c, void *data)
15651565
static void set_generation_v2(struct commit *c, timestamp_t t, void *data)
15661566
{
15671567
struct commit_graph_data *g = commit_graph_data_at(c);
1568-
g->generation = (uint32_t)t;
1568+
g->generation = t;
15691569
}
15701570

15711571
static void compute_generation_numbers(struct write_commit_graph_context *ctx)

t/t5328-commit-graph-64bit-time.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,13 @@ test_expect_success 'set up and verify repo with generation data overflow chunk'
6363

6464
graph_git_behavior 'overflow 2' repo left right
6565

66+
test_expect_success 'single commit with generation data exceeding UINT32_MAX' '
67+
git init repo-uint32-max &&
68+
cd repo-uint32-max &&
69+
test_commit --date "@4294967297 +0000" 1 &&
70+
git commit-graph write --reachable &&
71+
graph_read_expect 1 "generation_data" &&
72+
git commit-graph verify
73+
'
74+
6675
test_done

0 commit comments

Comments
 (0)