threads / patch / 15718

patch, 2 partsFix the UnpackedObjectCache hash function to prevent overflow

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

## tl;dr

2 messages between Sep 30, 2008 and Sep 30, 2008. Diffs are folded; open one to read it.

replies: 1people: 1as markdown or json

Shawn O. Pearce· Sep 30, 2008, 07:47 UTC · lore

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(-)
Show changes to org.spearce.jgit/src/org/spearce/jgit/lib/UnpackedObjectCache.java +3 −3
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
Shawn O. Pearce· Sep 30, 2008, 07:47 UTC · re: Shawn O. Pearce · lore

[JGIT PATCH 2/2] ObjectId: Convert StringIndexOutOfBounds to IllegalArgumentException

If parsing an ObjectId fromString fails because the string is too short we don't want to throw StringIndexOutOfBoundsException with a trace that leads back to the error handling code; instead we should throw what the caller expects us to throw, which is IllegalArgumentException.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../src/org/spearce/jgit/lib/MutableObjectId.java  |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
Show changes to org.spearce.jgit/src/org/spearce/jgit/lib/MutableObjectId.java +2 −0
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/MutableObjectId.java b/org.spearce.jgit/src/org/spearce/jgit/lib/MutableObjectId.java
index f88d8cb..5b16345 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/MutableObjectId.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/MutableObjectId.java
@@ -161,6 +161,8 @@ private void fromHexString(final byte[] bs, int p) {
 				throw new IllegalArgumentException("Invalid id: " + str);
 			} catch (UnsupportedEncodingException e2) {
 				throw new IllegalArgumentException("Invalid id");
+			} catch (StringIndexOutOfBoundsException e2) {
+				throw new IllegalArgumentException("Invalid id");
 			}
 		}
 	}
-- 
1.6.0.2.463.g7f0eb

← back to recent threads