Skip to content

Commit

Permalink
Fix github issue #27: ArrayIndexOutOfBoundsException in src/java/vold…
Browse files Browse the repository at this point in the history
…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
ijuma committed Jul 3, 2009
1 parent 218f057 commit 6b5aeb7
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/java/voldemort/routing/ConsistentRoutingStrategy.java
Expand Up @@ -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]))
Expand Down

0 comments on commit 6b5aeb7

Please sign in to comment.