Skip to content
Browse files

Fix github issue #27: ArrayIndexOutOfBoundsException in src/java/vold…

…emort/routing/ConsistentRoutingStrategy.java

Use a modified abs that always returns a non-negative value instead of Math.abs. This change is backwards compatible because the only hash value that is treated differently is Integer.MIN_VALUE and before the change this would cause a AIOOBE to be thrown.
  • Loading branch information...
1 parent 218f057 commit 6b5aeb7242a8ae814b18a5b13455e6c751c745a0 @ijuma ijuma committed
Showing with 14 additions and 1 deletion.
  1. +14 −1 src/java/voldemort/routing/ConsistentRoutingStrategy.java
View
15 src/java/voldemort/routing/ConsistentRoutingStrategy.java
@@ -75,9 +75,22 @@ public ConsistentRoutingStrategy(HashFunction hash, Collection<Node> nodes, int
}
}
+ /**
+ * A modified version of abs that always returns a non-negative value.
+ * Math.abs returns Integer.MIN_VALUE if a == Integer.MIN_VALUE and this
+ * method returns Integer.MAX_VALUE in that case.
+ */
+ private static int abs(int a) {
+ if(a >= 0)
+ return a;
+ else if(a != Integer.MIN_VALUE)
+ return -a;
+ return Integer.MAX_VALUE;
+ }
+
public List<Node> routeRequest(byte[] key) {
List<Node> preferenceList = new ArrayList<Node>(numReplicas);
- int index = Math.abs(hash.hash(key)) % this.partitionToNode.length;
+ int index = abs(hash.hash(key)) % this.partitionToNode.length;
for(int i = 0; i < partitionToNode.length; i++) {
// add this one if we haven't already
if(!preferenceList.contains(partitionToNode[index]))

0 comments on commit 6b5aeb7

Please sign in to comment.
Something went wrong with that request. Please try again.