threads / patch / 66904

patch, 2 partscombine-diff: fix patches for more than 32 parents

Subject: [PATCH 0/2] combine-diff: fix patches for more than 32 parents

## tl;dr

An int shift in coalesce_lines() sets the wrong parent bits, and a second patch makes oversized octopus merges report an error instead of printing garbage. Read the story.

replies: 2people: 1as markdown or json

Aniket Gupta· Nov 21, 2026, 10:42 UTC · lore

This series fixes a bug that Sami Jawhar reported [1]: the combined diff of a merge with more than 32 parents puts lines in the wrong columns, and with 64 parents it shows no hunks at all.

Patch 1 is the actual fix. The shift that sets the bit of a parent in the mask of a lost line was an int shift, so the 33rd parent and later got the bit of another parent.

Patch 2 is for merges that are still too big for the masks which is more than 62 parents where long has 64 bits. Instead of printing wrong output, it now prints an error and skips the patch. I made it carry on and not die. Now "git log --cc" will not stop halfway on a big octopus merge. I am happy to change this if people prefer. On Windows, long has 32 bits, so the limit is 30 and the tests for more parents are skipped.

This is based on maint since the bug is old. The test suite passes and CI on my fork is fine on Windows, macOS and Linux.

[1] https://lore.kernel.org/git/CAOJ8FdczW9KxFknceHodV0jmhyEB24OMtqQUmhkP3E2vfsmgag@mail.gmail.com/
Aniket Gupta (2):
  combine-diff: fix the shift for the 33rd parent and later
  combine-diff: do not show a patch for too many parents
 combine-diff.c           | 18 ++++++++++--
 t/t4038-diff-combined.sh | 60 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+), 2 deletions(-)
-- 
2.54.0 (Apple Git-157)
Aniket Gupta· Nov 21, 2026, 10:42 UTC · re: Aniket Gupta · lore

[PATCH 1/2] combine-diff: fix the shift for the 33rd parent and later

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(-)
Show changes to 2 files +25 −1

combine-diff.c, t/t4038-diff-combined.sh

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)
Aniket Gupta· Nov 21, 2026, 10:42 UTC · re: Aniket Gupta · lore

[PATCH 2/2] combine-diff: do not show a patch for too many parents

The masks used to show a combined diff patch hold one bit for each parent, plus two more bits for markers in the flags of a line. With an unsigned long that is enough for 62 parents, or 30 where long has 32 bits. For a merge with more parents the shifts are undefined, so the hunks are either missing or shown with the wrong columns, and nothing tells the user.

Report an error instead and skip the patch for such a merge. Do not die, so that a "git log --cc" through a history with a big octopus merge is not cut short. The raw format does not use the masks and is shown as before.

Add tests for the largest number of parents that works and for one more than that. The first is skipped where long is not 64 bits.

Signed-off-by: Aniket Gupta <aniketgupta3001@gmail.com>
---
 combine-diff.c           | 16 +++++++++++++++-
 t/t4038-diff-combined.sh | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 1 deletion(-)
Show changes to 2 files +51 −1

combine-diff.c, t/t4038-diff-combined.sh

diff --git a/combine-diff.c b/combine-diff.c
index 54d0aed6b2..414fec9cb8 100644
--- a/combine-diff.c
+++ b/combine-diff.c
@@ -136,6 +136,12 @@ struct sline {
 	unsigned long *p_lno;
 };
 
+/*
+ * The masks used to show a patch hold a bit for each parent, followed by
+ * the two marker bits described above.
+ */
+#define MAX_PATCH_PARENTS ((int)bitsizeof(unsigned long) - 2)
+
 static int match_string_spaces(const char *line1, int len1,
 			       const char *line2, int len2,
 			       long flags)
@@ -1491,6 +1497,7 @@ void diff_tree_combined(const struct object_id *oid,
 	struct combine_diff_path *p, *paths;
 	int i, num_paths, needsep, show_log_first, num_parent = parents->nr;
 	int need_generic_pathscan;
+	int show_patch;
 
 	if (opt->ignore_regex_nr)
 		die("combined diff and '%s' cannot be used together",
@@ -1515,6 +1522,13 @@ void diff_tree_combined(const struct object_id *oid,
 			       opt->line_termination);
 	}
 
+	show_patch = opt->output_format & DIFF_FORMAT_PATCH;
+	if (show_patch && num_parent > MAX_PATCH_PARENTS) {
+		error(_("cannot show a combined diff patch with more than %d parents"),
+		      MAX_PATCH_PARENTS);
+		show_patch = 0;
+	}
+
 	diffopts = *opt;
 	copy_pathspec(&diffopts.pathspec, &opt->pathspec);
 	diffopts.flags.allow_external = 0;
@@ -1616,7 +1630,7 @@ void diff_tree_combined(const struct object_id *oid,
 		else if (opt->output_format & DIFF_FORMAT_CALLBACK)
 			handle_combined_callback(opt, paths, num_parent, num_paths);
 
-		if (opt->output_format & DIFF_FORMAT_PATCH) {
+		if (show_patch) {
 			if (needsep)
 				printf("%s%c", diff_line_prefix(opt),
 				       opt->line_termination);
diff --git a/t/t4038-diff-combined.sh b/t/t4038-diff-combined.sh
index fcf7333d7b..ad670d76db 100755
--- a/t/t4038-diff-combined.sh
+++ b/t/t4038-diff-combined.sh
@@ -151,6 +151,42 @@ test_expect_success LONG_IS_64BIT 'check --cc with forty parents, two losing the
 	test_cmp expect actual
 '
 
+test_expect_success 'setup for --cc with many parents' '
+	blob=$(echo file | git hash-object --stdin -w) &&
+	result_tree=$(echo "100644 blob $blob	file" | git mktree) &&
+	many_trees= &&
+	for i in $(test_seq 1 63)
+	do
+		case $i in
+		2 | 40)
+			extra=common
+			;;
+		*)
+			extra=
+			;;
+		esac &&
+		blob=$(test_write_lines $extra file file$i | git hash-object --stdin -w) &&
+		many_trees="$many_trees$(echo "100644 blob $blob	file" | git mktree)$LF" || return 1
+	done
+'
+
+test_expect_success LONG_IS_64BIT 'check --cc with sixty-two parents' '
+	sixty_two_trees=$(echo "$many_trees" | sed -e 62q) &&
+	git diff --cc $result_tree $sixty_two_trees >out &&
+	# Check that the second and the fortieth column mark the line:
+	printf " -%37s-%22scommon\n" "" "" >expect &&
+	grep common out >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'refuse --cc patch with sixty-three parents' '
+	git diff --cc --patch-with-raw $result_tree $many_trees >out 2>err &&
+	test_grep "cannot show a combined diff patch with more than [0-9]* parents" err &&
+	# The raw output has a colon for each parent and is still shown:
+	test_grep "^:\{63\}[^:]" out &&
+	test_grep ! "^diff --cc" out
+'
+
 test_expect_success 'setup combined ignore spaces' '
 	git checkout main &&
 	>test &&
-- 
2.54.0 (Apple Git-157)

← back to recent threads