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

[JGIT PATCH 01/11] Fix more computation of integer average overflows

From
Shawn O. Pearce <spearce@spearce.org>
Date
Apr 29, 2009, 18:54 UTC
Message-ID
<1241031288-23437-2-git-send-email-spearce@spearce.org>
In-Reply-To
<1241031288-23437-1-git-send-email-spearce@spearce.org>

In 1d99aaab8e364c6ad722437e43c77fd54e13b071 Matthias Sohn pointed out that (low+high)/2 can overflow, so (low+high)>>>1 is a better choice when the result will be used as an array index.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 Unchanged.
 .../src/org/spearce/jgit/lib/PackIndexV1.java      |    2 +-
 .../src/org/spearce/jgit/lib/PackIndexV2.java      |    6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV1.java b/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV1.java
index fdaa094..0ad29e1 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV1.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV1.java
@@ -129,7 +129,7 @@ long findOffset(final AnyObjectId objId) {
 		int high = data.length / (4 + Constants.OBJECT_ID_LENGTH);
 		int low = 0;
 		do {
-			final int mid = (low + high) / 2;
+			final int mid = (low + high) >>> 1;
 			final int pos = ((4 + Constants.OBJECT_ID_LENGTH) * mid) + 4;
 			final int cmp = objId.compareTo(data, pos);
 			if (cmp < 0)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV2.java b/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV2.java
index eb56ed9..b539547 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV2.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/PackIndexV2.java
@@ -108,7 +108,7 @@ PackIndexV2(final InputStream fd) throws IOException {
 
 			final int intNameLen = (int) nameLen;
 			final byte[] raw = new byte[intNameLen];
-			final int[] bin = new int[intNameLen >> 2];
+			final int[] bin = new int[intNameLen >>> 2];
 			NB.readFully(fd, raw, 0, raw.length);
 			for (int i = 0; i < bin.length; i++)
 				bin[i] = NB.decodeInt32(raw, i << 2);
@@ -212,12 +212,12 @@ boolean hasCRC32Support() {
 
 	private int binarySearchLevelTwo(final AnyObjectId objId, final int levelOne) {
 		final int[] data = names[levelOne];
-		int high = offset32[levelOne].length >> 2;
+		int high = offset32[levelOne].length >>> 2;
 		if (high == 0)
 			return -1;
 		int low = 0;
 		do {
-			final int mid = (low + high) >> 1;
+			final int mid = (low + high) >>> 1;
 			final int mid4 = mid << 2;
 			final int cmp;
 
-- 
1.6.3.rc3.199.g24398
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 2 of 13 in “WindowCache rewrite... with tests”
  1. 00/11 WindowCache rewrite... with testsShawn O. Pearce, Apr 29, 2009
  2. 01/11 Fix more computation of integer average overflowsShawn O. Pearce, Apr 29, 2009
  3. 02/11 Fix performance problem recently introduced to DeltaPackedObjectLoaderShawn O. Pearce, Apr 29, 2009
  4. 03/11 Replace inefficient new String(String) constructor to silence FindBugsShawn O. Pearce, Apr 29, 2009
  5. 04/11 Replace inefficient new Long(long) constructor to silence FindBugsShawn O. Pearce, Apr 29, 2009
  6. 05/11 Change IndexPack to use ObjectIdSubclassMap instead of ObjectIdMapShawn O. Pearce, Apr 29, 2009
  7. 06/11 Use getCachedBytes in IndexPack to avoid an unnecessary copyShawn O. Pearce, Apr 29, 2009
  8. 07/11 Remove ObjectIdMap from the JGit libraryShawn O. Pearce, Apr 29, 2009
  9. 08/11 Don't use ByteWindows when checking pack file headers/footersShawn O. Pearce, Apr 29, 2009
  10. 09/11 Rewrite WindowCache to be easier to follow and maintainShawn O. Pearce, Apr 29, 2009
  11. 10/11 Add tests for WindowCache.reconfigure casesShawn O. Pearce, Apr 29, 2009
  12. 11/11 Add tests to increase coverage of WindowCacheShawn O. Pearce, Apr 29, 2009
  13. Robin RosenbergApr 29, 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.