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

[JGIT PATCH 1/2] Fix the UnpackedObjectCache hash function to prevent overflow

From
Shawn O. Pearce <spearce@spearce.org>
Date
Sep 30, 2008, 07:47 UTC
Message-ID
<1222760871-58768-1-git-send-email-spearce@spearce.org>

Sometimes we got negative array indexes inside of the cache due to WindowedFile.hash having a negative value assigned by the JVM. We now use only the lower 8 bits as the cache is fixed to at most 256 entries.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../org/spearce/jgit/lib/UnpackedObjectCache.java  |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/UnpackedObjectCache.java b/org.spearce.jgit/src/org/spearce/jgit/lib/UnpackedObjectCache.java
index 03cf674..ee6a680 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/UnpackedObjectCache.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/UnpackedObjectCache.java
@@ -48,9 +48,9 @@
 
 	private static int hash(final WindowedFile pack, final long position) {
 		int h = pack.hash + (int) position;
-		h += h >> 16;
-		h += h >> 8;
-		return h % CACHE_SZ;
+		h += h >>> 16;
+		h += h >>> 8;
+		return h & (CACHE_SZ - 1);
 	}
 
 	private static int maxByteCount;
-- 
1.6.0.2.463.g7f0eb
Next: Shawn O. Pearce
Message 1 of 2 in “Fix the UnpackedObjectCache hash function to prevent overflow”
  1. 1/2 Fix the UnpackedObjectCache hash function to prevent overflowShawn O. Pearce, Sep 30, 2008
  2. 2/2 ObjectId: Convert StringIndexOutOfBounds to IllegalArgumentExceptionShawn O. Pearce, Sep 30, 2008

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.