]> source.dussan.org Git - jgit.git/commitdiff
Fix ArrayIndexOutOfBounds on non-square exact rename matrix 60/1260/1
authorShawn O. Pearce <spearce@spearce.org>
Fri, 6 Aug 2010 16:33:55 +0000 (09:33 -0700)
committerShawn O. Pearce <spearce@spearce.org>
Fri, 6 Aug 2010 16:39:10 +0000 (09:39 -0700)
If the exact rename matrix for a particular ObjectId isn't square we
crashed with an ArrayIndexOutOfBoundsException because the matrix
entries were encoded backwards.  The encode function accepts the
source (aka deleted) index first, not second.  Add a unit test to
cover this non-square case to ensure we don't have this regression
in the future.

Change-Id: I5b005e5093e1f00de2e3ec104e27ab6820203566
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
org.eclipse.jgit.test/tst/org/eclipse/jgit/diff/RenameDetectorTest.java
org.eclipse.jgit/src/org/eclipse/jgit/diff/RenameDetector.java

index 6024c7699896da978e0dd3371e8c8e2f0bc4ff23..b5d94c02c7792f7ec018e15294a7d94b71c95fcd 100644 (file)
@@ -173,17 +173,20 @@ public class RenameDetectorTest extends RepositoryTestCase {
 
                DiffEntry c = DiffEntry.add("c.txt", foo);
                DiffEntry d = DiffEntry.delete("d.txt", foo);
+               DiffEntry e = DiffEntry.add("the_e_file.txt", foo);
 
                // Add out of order to avoid first-match succeeding
                rd.add(a);
                rd.add(d);
+               rd.add(e);
                rd.add(b);
                rd.add(c);
 
                List<DiffEntry> entries = rd.compute();
-               assertEquals(2, entries.size());
+               assertEquals(3, entries.size());
                assertRename(d, c, 100, entries.get(0));
                assertRename(b, a, 100, entries.get(1));
+               assertCopy(d, e, 100, entries.get(2));
        }
 
        public void testExactRename_OneDeleteManyAdds() throws Exception {
index cf5615a1cb4e83700f869b5563818a1c0656d9c3..ad37a73721194b796141f0831ae422377700d086 100644 (file)
@@ -390,14 +390,14 @@ public class RenameDetector {
                                List<DiffEntry> dels = (List<DiffEntry>) o;
                                long[] matrix = new long[dels.size() * adds.size()];
                                int mNext = 0;
-                               for (int addIdx = 0; addIdx < adds.size(); addIdx++) {
-                                       String addedName = adds.get(addIdx).newName;
+                               for (int delIdx = 0; delIdx < dels.size(); delIdx++) {
+                                       String deletedName = dels.get(delIdx).oldName;
 
-                                       for (int delIdx = 0; delIdx < dels.size(); delIdx++) {
-                                               String deletedName = dels.get(delIdx).oldName;
+                                       for (int addIdx = 0; addIdx < adds.size(); addIdx++) {
+                                               String addedName = adds.get(addIdx).newName;
 
                                                int score = SimilarityRenameDetector.nameScore(addedName, deletedName);
-                                               matrix[mNext] = SimilarityRenameDetector.encode(score, addIdx, delIdx);
+                                               matrix[mNext] = SimilarityRenameDetector.encode(score, delIdx, addIdx);
                                                mNext++;
                                        }
                                }