]> git.ipfire.org Git - thirdparty/git.git/commit - commit-graph.c
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)
commitd3af1c193d4d62668a9d7ea98e2ef3771ac4e65a
tree98ac9fc6588baeebe0af3d5272e89a6107d447e9
parentcbfe360b140fe92d9c4a763bf630c3b8ba431522
commit-graph: fix truncated generation numbers

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