[PATCH 1/2] combine-diff: fix the shift for the 33rd parent and later
- From
- Aniket Gupta <aniketgupta3001@gmail.com>
- Date
- Nov 21, 2026, 10:42 UTC
- Message-ID
- <20261121104234.98386-2-aniketgupta3001@gmail.com>
- In-Reply-To
- <20261121104234.98386-1-aniketgupta3001@gmail.com>
coalesce_lines() records which parents lost a line by setting a bit in the parent_map of that line. The bit is computed with 1<<parent, which is an int shift. For the 33rd parent and later the shift count is 32 or more, so the result is undefined, and in practice the bit for parent minus 32 is set instead. The line is then shown in the wrong column of the combined diff, and the hunk header can disagree with the lines in the hunk.
Use 1UL<<parent, like the other masks in this file. parent_map is an unsigned long.
Add a test where two of forty parents lose the same line. It is skipped where long is not 64 bits, as the masks cannot hold that many parents there.
Reported-by: Sami Jawhar <sami@theorem.dev> Signed-off-by: Aniket Gupta <aniketgupta3001@gmail.com> --- combine-diff.c | 2 +- t/t4038-diff-combined.sh | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-)
diff --git a/combine-diff.c b/combine-diff.c index 717d537688..54d0aed6b2 100644 --- a/combine-diff.c +++ b/combine-diff.c @@ -246,7 +246,7 @@ static struct lline *coalesce_lines(struct lline *base, int *lenbase, j--; while (i != 0 || j != 0) { if (directions[i][j] == MATCH) { - baseend->parent_map |= 1<<parent; + baseend->parent_map |= 1UL << parent; baseend = baseend->prev; newend = newend->prev; i--; diff --git a/t/t4038-diff-combined.sh b/t/t4038-diff-combined.sh index e11b711388..fcf7333d7b 100755 --- a/t/t4038-diff-combined.sh +++ b/t/t4038-diff-combined.sh @@ -127,6 +127,30 @@ test_expect_success 'check --cc --raw with forty trees' ' test_grep "^::::::::::::::::::::::::::::::::::::::::[^:]" out ' +test_expect_success LONG_IS_64BIT 'check --cc with forty parents, two losing the same line' ' + blob=$(echo file | git hash-object --stdin -w) && + result_tree=$(echo "100644 blob $blob file" | git mktree) && + parent_trees= && + for i in $(test_seq 1 40) + do + case $i in + 2 | 40) + extra=common + ;; + *) + extra= + ;; + esac && + blob=$(test_write_lines $extra file file$i | git hash-object --stdin -w) && + parent_trees="$parent_trees$(echo "100644 blob $blob file" | git mktree)$LF" || return 1 + done && + git diff --cc $result_tree $parent_trees >out && + # Check that the second and the fortieth column mark the line: + printf " -%37s-common\n" "" >expect && + grep common out >actual && + test_cmp expect actual +' + test_expect_success 'setup combined ignore spaces' ' git checkout main && >test &&
-- 2.54.0 (Apple Git-157)