Skip to content

Commit 3aab60b

Browse files
committed
Merge branch 'jk/diffcore-rename-duplicate' into maint
A corrupt input to "git diff -M" can cause us to segfault. * jk/diffcore-rename-duplicate: diffcore-rename: avoid processing duplicate destinations diffcore-rename: split locate_rename_dst into two functions
2 parents ae8ada4 + 4d6be03 commit 3aab60b

File tree

2 files changed

+110
-13
lines changed

2 files changed

+110
-13
lines changed

diffcore-rename.c

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ static struct diff_rename_dst {
1515
} *rename_dst;
1616
static int rename_dst_nr, rename_dst_alloc;
1717

18-
static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
19-
int insert_ok)
18+
static int find_rename_dst(struct diff_filespec *two)
2019
{
2120
int first, last;
2221

@@ -27,16 +26,33 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
2726
struct diff_rename_dst *dst = &(rename_dst[next]);
2827
int cmp = strcmp(two->path, dst->two->path);
2928
if (!cmp)
30-
return dst;
29+
return next;
3130
if (cmp < 0) {
3231
last = next;
3332
continue;
3433
}
3534
first = next+1;
3635
}
37-
/* not found */
38-
if (!insert_ok)
39-
return NULL;
36+
return -first - 1;
37+
}
38+
39+
static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two)
40+
{
41+
int ofs = find_rename_dst(two);
42+
return ofs < 0 ? NULL : &rename_dst[ofs];
43+
}
44+
45+
/*
46+
* Returns 0 on success, -1 if we found a duplicate.
47+
*/
48+
static int add_rename_dst(struct diff_filespec *two)
49+
{
50+
int first = find_rename_dst(two);
51+
52+
if (first >= 0)
53+
return -1;
54+
first = -first - 1;
55+
4056
/* insert to make it at "first" */
4157
ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
4258
rename_dst_nr++;
@@ -46,7 +62,7 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
4662
rename_dst[first].two = alloc_filespec(two->path);
4763
fill_filespec(rename_dst[first].two, two->sha1, two->sha1_valid, two->mode);
4864
rename_dst[first].pair = NULL;
49-
return &(rename_dst[first]);
65+
return 0;
5066
}
5167

5268
/* Table of rename/copy src files */
@@ -450,8 +466,12 @@ void diffcore_rename(struct diff_options *options)
450466
else if (!DIFF_OPT_TST(options, RENAME_EMPTY) &&
451467
is_empty_blob_sha1(p->two->sha1))
452468
continue;
453-
else
454-
locate_rename_dst(p->two, 1);
469+
else if (add_rename_dst(p->two) < 0) {
470+
warning("skipping rename detection, detected"
471+
" duplicate destination '%s'",
472+
p->two->path);
473+
goto cleanup;
474+
}
455475
}
456476
else if (!DIFF_OPT_TST(options, RENAME_EMPTY) &&
457477
is_empty_blob_sha1(p->one->sha1))
@@ -582,8 +602,7 @@ void diffcore_rename(struct diff_options *options)
582602
* We would output this create record if it has
583603
* not been turned into a rename/copy already.
584604
*/
585-
struct diff_rename_dst *dst =
586-
locate_rename_dst(p->two, 0);
605+
struct diff_rename_dst *dst = locate_rename_dst(p->two);
587606
if (dst && dst->pair) {
588607
diff_q(&outq, dst->pair);
589608
pair_to_free = p;
@@ -613,8 +632,7 @@ void diffcore_rename(struct diff_options *options)
613632
*/
614633
if (DIFF_PAIR_BROKEN(p)) {
615634
/* broken delete */
616-
struct diff_rename_dst *dst =
617-
locate_rename_dst(p->one, 0);
635+
struct diff_rename_dst *dst = locate_rename_dst(p->one);
618636
if (dst && dst->pair)
619637
/* counterpart is now rename/copy */
620638
pair_to_free = p;

t/t4058-diff-duplicates.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/bin/sh
2+
3+
test_description='test tree diff when trees have duplicate entries'
4+
. ./test-lib.sh
5+
6+
# make_tree_entry <mode> <mode> <sha1>
7+
#
8+
# We have to rely on perl here because not all printfs understand
9+
# hex escapes (only octal), and xxd is not portable.
10+
make_tree_entry () {
11+
printf '%s %s\0' "$1" "$2" &&
12+
perl -e 'print chr(hex($_)) for ($ARGV[0] =~ /../g)' "$3"
13+
}
14+
15+
# Like git-mktree, but without all of the pesky sanity checking.
16+
# Arguments come in groups of three, each group specifying a single
17+
# tree entry (see make_tree_entry above).
18+
make_tree () {
19+
while test $# -gt 2; do
20+
make_tree_entry "$1" "$2" "$3"
21+
shift; shift; shift
22+
done |
23+
git hash-object -w -t tree --stdin
24+
}
25+
26+
# this is kind of a convoluted setup, but matches
27+
# a real-world case. Each tree contains four entries
28+
# for the given path, one with one sha1, and three with
29+
# the other. The first tree has them split across
30+
# two subtrees (which are themselves duplicate entries in
31+
# the root tree), and the second has them all in a single subtree.
32+
test_expect_success 'create trees with duplicate entries' '
33+
blob_one=$(echo one | git hash-object -w --stdin) &&
34+
blob_two=$(echo two | git hash-object -w --stdin) &&
35+
inner_one_a=$(make_tree \
36+
100644 inner $blob_one
37+
) &&
38+
inner_one_b=$(make_tree \
39+
100644 inner $blob_two \
40+
100644 inner $blob_two \
41+
100644 inner $blob_two
42+
) &&
43+
outer_one=$(make_tree \
44+
040000 outer $inner_one_a \
45+
040000 outer $inner_one_b
46+
) &&
47+
inner_two=$(make_tree \
48+
100644 inner $blob_one \
49+
100644 inner $blob_two \
50+
100644 inner $blob_two \
51+
100644 inner $blob_two
52+
) &&
53+
outer_two=$(make_tree \
54+
040000 outer $inner_two
55+
) &&
56+
git tag one $outer_one &&
57+
git tag two $outer_two
58+
'
59+
60+
test_expect_success 'diff-tree between trees' '
61+
{
62+
printf ":000000 100644 $_z40 $blob_two A\touter/inner\n" &&
63+
printf ":000000 100644 $_z40 $blob_two A\touter/inner\n" &&
64+
printf ":000000 100644 $_z40 $blob_two A\touter/inner\n" &&
65+
printf ":100644 000000 $blob_two $_z40 D\touter/inner\n" &&
66+
printf ":100644 000000 $blob_two $_z40 D\touter/inner\n" &&
67+
printf ":100644 000000 $blob_two $_z40 D\touter/inner\n"
68+
} >expect &&
69+
git diff-tree -r --no-abbrev one two >actual &&
70+
test_cmp expect actual
71+
'
72+
73+
test_expect_success 'diff-tree with renames' '
74+
# same expectation as above, since we disable rename detection
75+
git diff-tree -M -r --no-abbrev one two >actual &&
76+
test_cmp expect actual
77+
'
78+
79+
test_done

0 commit comments

Comments
 (0)