From 7a0c126d5fc4789eebebf41331ae70d7492b635c Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sun, 10 Oct 2010 13:08:50 -0700 Subject: [PATCH 1/2] Fix infinite loop in PatienceDiff Certain inputs caused an infinite loop because the prior match data couldn't be used as expected. Rather than incrementing the match pointer before looking at an element, do it after, so the loop breaks when we wrap around to the starting point. Change-Id: Ieab28bb3485a914eeddc68aa38c256f255dd778c Signed-off-by: Shawn O. Pearce --- .../org/eclipse/jgit/diff/PatienceDiffIndex.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/PatienceDiffIndex.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/PatienceDiffIndex.java index 27cf9252e..042abd2d7 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/PatienceDiffIndex.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/PatienceDiffIndex.java @@ -244,7 +244,7 @@ final class PatienceDiffIndex { private void scanA() { int ptr = region.beginA; final int end = region.endA; - int pLast = pBegin - 1; + int pLast = pBegin; SCAN: while (ptr < end) { final int tIdx = hash(a, ptr); @@ -276,12 +276,7 @@ final class PatienceDiffIndex { // fact that pCommon is sorted by B, and its likely that // matches in A appear in the same order as they do in B. // - for (int pIdx = pLast + 1;; pIdx++) { - if (pIdx == pEnd) - pIdx = pBegin; - else if (pIdx == pLast) - break; - + for (int pIdx = pLast;;) { final long priorRec = pCommon[pIdx]; final int priorB = bOf(priorRec); if (bs < priorB) @@ -291,6 +286,12 @@ final class PatienceDiffIndex { pLast = pIdx; continue SCAN; } + + pIdx++; + if (pIdx == pEnd) + pIdx = pBegin; + if (pIdx == pLast) + break; } } From 4fc50df97df55b1090d8ef5717805003527742cd Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sun, 10 Oct 2010 13:36:39 -0700 Subject: [PATCH 2/2] Fix empty block corner case in PatienceDiff There is a corner case where we get an EMPTY region during recursion, but we didn't expect to receive that. Its harmless to ignore the region since the region is empty and has no content, so do so rather than throwing an exception Change-Id: I50dcec81ecba763072bb739adfab5879fb48b23a Signed-off-by: Shawn O. Pearce --- org.eclipse.jgit/src/org/eclipse/jgit/diff/PatienceDiff.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/diff/PatienceDiff.java b/org.eclipse.jgit/src/org/eclipse/jgit/diff/PatienceDiff.java index 571a498ae..dfbf1a49b 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/diff/PatienceDiff.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/diff/PatienceDiff.java @@ -182,6 +182,8 @@ public class PatienceDiff extends DiffAlgorithm { break; case EMPTY: + break; + default: throw new IllegalStateException(); }