git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH (RESEND) 3/3] Fix DirCache.findEntry to work on an empty cache

From
Shawn O. Pearce <spearce@spearce.org>
Date
Sep 1, 2009, 23:16 UTC
Message-ID
<1251847010-9992-3-git-send-email-spearce@spearce.org>
In-Reply-To
<1251847010-9992-2-git-send-email-spearce@spearce.org>

If the cache has no entries, we want to return -1 rather than throw ArrayIndexOutOfBoundsException. This binary search loop was stolen from some other code which contained a test before the loop to see if the collection was empty or not, but we failed to include that here.

Flipping the loop around to a standard while loop ensures we test the condition properly first.

Signed-off-by: Shawn O. Pearce <sop@google.com>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../spearce/jgit/dircache/DirCacheBasicTest.java   |    6 ++++++
 .../src/org/spearce/jgit/dircache/DirCache.java    |    6 ++----
 2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/dircache/DirCacheBasicTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/dircache/DirCacheBasicTest.java
index b3097ac..4d737c0 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/dircache/DirCacheBasicTest.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/dircache/DirCacheBasicTest.java
@@ -39,6 +39,7 @@
 
 import java.io.File;
 
+import org.spearce.jgit.lib.Constants;
 import org.spearce.jgit.lib.RepositoryTestCase;
 
 public class DirCacheBasicTest extends RepositoryTestCase {
@@ -182,4 +183,9 @@ public void testBuildThenClear() throws Exception {
 		assertEquals(0, dc.getEntryCount());
 	}
 
+	public void testFindOnEmpty() throws Exception {
+		final DirCache dc = DirCache.newInCore();
+		final byte[] path = Constants.encode("a");
+		assertEquals(-1, dc.findEntry(path, path.length));
+	}
 }
diff --git a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java
index bfb7925..9f0810a 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/dircache/DirCache.java
@@ -583,8 +583,6 @@ public void unlock() {
 	 *         information. If < 0 the entry does not exist in the index.
 	 */
 	public int findEntry(final String path) {
-		if (entryCnt == 0)
-			return -1;
 		final byte[] p = Constants.encode(path);
 		return findEntry(p, p.length);
 	}
@@ -592,7 +590,7 @@ public int findEntry(final String path) {
 	int findEntry(final byte[] p, final int pLen) {
 		int low = 0;
 		int high = entryCnt;
-		do {
+		while (low < high) {
 			int mid = (low + high) >>> 1;
 			final int cmp = cmp(p, pLen, sortedEntries[mid]);
 			if (cmp < 0)
@@ -603,7 +601,7 @@ else if (cmp == 0) {
 				return mid;
 			} else
 				low = mid + 1;
-		} while (low < high);
+		}
 		return -(low + 1);
 	}
 
-- 
1.6.4.1.341.gf2a44
Previous: Shawn O. PearceNext: Robin Rosenberg
Message 3 of 7 in “Allow RefUpdate.setExpectedOldObjectId to accept RevCommit”
  1. 1/3 Allow RefUpdate.setExpectedOldObjectId to accept RevCommitShawn O. Pearce, Sep 1, 2009
  2. 2/3 Work around Sun javac compiler error in RefUpdateShawn O. Pearce, Sep 1, 2009
  3. 3/3 Fix DirCache.findEntry to work on an empty cacheShawn O. Pearce, Sep 1, 2009
  4. Robin RosenbergSep 3, 2009
  5. Shawn O. PearceSep 3, 2009
  6. Nicolas PitreSep 4, 2009
  7. Shawn O. PearceSep 1, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.