Menu

#8 ArrayIndexOutOfBoundsException in HashMap.get(2147483648L)

v1.2
open
nobody
Classes (14)
5
2014-10-09
2006-08-21
Daniel
No

The following will produce an
ArrayIndexOutOfBoundsException:
LongKeyOpenHashMap m2 = new LongKeyOpenHashMap(64);
m2.get(2147483648L);

It's because Math.abs() can return a negative number,
when the input is Integer.MIN_VALUE.

When the input of get is 2147483648L, the following
operations will be performed:
1- hash using DefaultLongHashFunction (v = 2147483648L)
return (int)(v ^ (v >>> 32));
result: -2147483648 (that's because you cast
2147483648L in an int)
2- call Math.abs(-2147483648). This behavior is
documented in Math.abs javadoc.
result: -2147483648
3-call remainder operator
int i = h % keys.length;
result: depends on keys.length, but the result can be
negative. See
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4639626
for details on how the remainder operator works.

Then, when states[i] is called, there is an
ArrayIndexOutOfBoundsException exception.

Discussion


Log in to post a comment.