Merge branch 'jk/diffcore-rename-duplicate'

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
This commit is contained in:
Junio C Hamano 2015-03-10 13:52:38 -07:00
commit 2d659f7d6e
2 changed files with 110 additions and 13 deletions

View File

@ -15,8 +15,7 @@ static struct diff_rename_dst {
} *rename_dst; } *rename_dst;
static int rename_dst_nr, rename_dst_alloc; static int rename_dst_nr, rename_dst_alloc;
static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two, static int find_rename_dst(struct diff_filespec *two)
int insert_ok)
{ {
int first, last; int first, last;
@ -27,16 +26,33 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
struct diff_rename_dst *dst = &(rename_dst[next]); struct diff_rename_dst *dst = &(rename_dst[next]);
int cmp = strcmp(two->path, dst->two->path); int cmp = strcmp(two->path, dst->two->path);
if (!cmp) if (!cmp)
return dst; return next;
if (cmp < 0) { if (cmp < 0) {
last = next; last = next;
continue; continue;
} }
first = next+1; first = next+1;
} }
/* not found */ return -first - 1;
if (!insert_ok) }
return NULL;
static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two)
{
int ofs = find_rename_dst(two);
return ofs < 0 ? NULL : &rename_dst[ofs];
}
/*
* Returns 0 on success, -1 if we found a duplicate.
*/
static int add_rename_dst(struct diff_filespec *two)
{
int first = find_rename_dst(two);
if (first >= 0)
return -1;
first = -first - 1;
/* insert to make it at "first" */ /* insert to make it at "first" */
ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc); ALLOC_GROW(rename_dst, rename_dst_nr + 1, rename_dst_alloc);
rename_dst_nr++; rename_dst_nr++;
@ -46,7 +62,7 @@ static struct diff_rename_dst *locate_rename_dst(struct diff_filespec *two,
rename_dst[first].two = alloc_filespec(two->path); rename_dst[first].two = alloc_filespec(two->path);
fill_filespec(rename_dst[first].two, two->sha1, two->sha1_valid, two->mode); fill_filespec(rename_dst[first].two, two->sha1, two->sha1_valid, two->mode);
rename_dst[first].pair = NULL; rename_dst[first].pair = NULL;
return &(rename_dst[first]); return 0;
} }
/* Table of rename/copy src files */ /* Table of rename/copy src files */
@ -450,8 +466,12 @@ void diffcore_rename(struct diff_options *options)
else if (!DIFF_OPT_TST(options, RENAME_EMPTY) && else if (!DIFF_OPT_TST(options, RENAME_EMPTY) &&
is_empty_blob_sha1(p->two->sha1)) is_empty_blob_sha1(p->two->sha1))
continue; continue;
else else if (add_rename_dst(p->two) < 0) {
locate_rename_dst(p->two, 1); warning("skipping rename detection, detected"
" duplicate destination '%s'",
p->two->path);
goto cleanup;
}
} }
else if (!DIFF_OPT_TST(options, RENAME_EMPTY) && else if (!DIFF_OPT_TST(options, RENAME_EMPTY) &&
is_empty_blob_sha1(p->one->sha1)) is_empty_blob_sha1(p->one->sha1))
@ -582,8 +602,7 @@ void diffcore_rename(struct diff_options *options)
* We would output this create record if it has * We would output this create record if it has
* not been turned into a rename/copy already. * not been turned into a rename/copy already.
*/ */
struct diff_rename_dst *dst = struct diff_rename_dst *dst = locate_rename_dst(p->two);
locate_rename_dst(p->two, 0);
if (dst && dst->pair) { if (dst && dst->pair) {
diff_q(&outq, dst->pair); diff_q(&outq, dst->pair);
pair_to_free = p; pair_to_free = p;
@ -613,8 +632,7 @@ void diffcore_rename(struct diff_options *options)
*/ */
if (DIFF_PAIR_BROKEN(p)) { if (DIFF_PAIR_BROKEN(p)) {
/* broken delete */ /* broken delete */
struct diff_rename_dst *dst = struct diff_rename_dst *dst = locate_rename_dst(p->one);
locate_rename_dst(p->one, 0);
if (dst && dst->pair) if (dst && dst->pair)
/* counterpart is now rename/copy */ /* counterpart is now rename/copy */
pair_to_free = p; pair_to_free = p;

79
t/t4058-diff-duplicates.sh Executable file
View File

@ -0,0 +1,79 @@
#!/bin/sh
test_description='test tree diff when trees have duplicate entries'
. ./test-lib.sh
# make_tree_entry <mode> <mode> <sha1>
#
# We have to rely on perl here because not all printfs understand
# hex escapes (only octal), and xxd is not portable.
make_tree_entry () {
printf '%s %s\0' "$1" "$2" &&
perl -e 'print chr(hex($_)) for ($ARGV[0] =~ /../g)' "$3"
}
# Like git-mktree, but without all of the pesky sanity checking.
# Arguments come in groups of three, each group specifying a single
# tree entry (see make_tree_entry above).
make_tree () {
while test $# -gt 2; do
make_tree_entry "$1" "$2" "$3"
shift; shift; shift
done |
git hash-object -w -t tree --stdin
}
# this is kind of a convoluted setup, but matches
# a real-world case. Each tree contains four entries
# for the given path, one with one sha1, and three with
# the other. The first tree has them split across
# two subtrees (which are themselves duplicate entries in
# the root tree), and the second has them all in a single subtree.
test_expect_success 'create trees with duplicate entries' '
blob_one=$(echo one | git hash-object -w --stdin) &&
blob_two=$(echo two | git hash-object -w --stdin) &&
inner_one_a=$(make_tree \
100644 inner $blob_one
) &&
inner_one_b=$(make_tree \
100644 inner $blob_two \
100644 inner $blob_two \
100644 inner $blob_two
) &&
outer_one=$(make_tree \
040000 outer $inner_one_a \
040000 outer $inner_one_b
) &&
inner_two=$(make_tree \
100644 inner $blob_one \
100644 inner $blob_two \
100644 inner $blob_two \
100644 inner $blob_two
) &&
outer_two=$(make_tree \
040000 outer $inner_two
) &&
git tag one $outer_one &&
git tag two $outer_two
'
test_expect_success 'diff-tree between trees' '
{
printf ":000000 100644 $_z40 $blob_two A\touter/inner\n" &&
printf ":000000 100644 $_z40 $blob_two A\touter/inner\n" &&
printf ":000000 100644 $_z40 $blob_two A\touter/inner\n" &&
printf ":100644 000000 $blob_two $_z40 D\touter/inner\n" &&
printf ":100644 000000 $blob_two $_z40 D\touter/inner\n" &&
printf ":100644 000000 $blob_two $_z40 D\touter/inner\n"
} >expect &&
git diff-tree -r --no-abbrev one two >actual &&
test_cmp expect actual
'
test_expect_success 'diff-tree with renames' '
# same expectation as above, since we disable rename detection
git diff-tree -M -r --no-abbrev one two >actual &&
test_cmp expect actual
'
test_done