gnu.trove classes throws java.lang.ArithmeticException: divide by zero

164 Views Asked by At

My application uses gnu.trove version 2.0.3. Recently it is throwing divide by zero exceptions in different areas of the gnu.trove library code

e.g.: 1.

java.lang.ArithmeticException: divide by zero
at gnu.trove.TIntHash.index(TIntHash.java:201)
at gnu.trove.TIntObjectHashMap.get(TIntObjectHashMap.java:204)

2.

java.lang.ArithmeticException: divide by zero
at gnu.trove.TIntHash.insertionIndex(TIntHash.
at gnu.trove.TIntObjectHashMap.put(TIntObjectHashMap.java:153)

3.

java.lang.ArithmeticException: divide by zero
at gnu.trove.TPrimitiveHash.capacity(TPrimitiveHash.java:99)
at gnu.trove.THash.postInsertHook(THash.java:358)
at gnu.trove.TIntLongHashMap.put(TIntLongHashMap.java:165)

This is the snippet of the application code that throws divide by zero exceptions in one scenario:

TIntLongHashMap testMap = new TIntLongHashMap();
TIntLongHashMap test1Map = new TIntLongHashMap();
.
.
.
TIntLongIterator iter = test1Map.iterator();
for (int i = test1Map.size(); i-- > 0; )
{
        iter.advance();
        **testMap.put(iter.key(), iter.value());**
}       

From the stack trace, I looked at the lines of code that are throwing these exceptions inside these library classes: 1. at gnu.trove.TIntHash.index(TIntHash.java:201)

protected int index(int val)
/ / {
/ 185 / byte[] states = _states;
/ 186 / int[] set = _set;
/ 187 / int length = states.length;
/ 188 / int hash = _hashingStrategy.computeHashCode(val) & 0x7FFFFFFF;
/ 189 / int index = hash % length;
/ /
/ 191 / if ((states[index] != 0) && ((states[index] == 2) || (set[index] != val)))
/ / {
/ /
/ 194 / int probe = 1 + hash % (length - 2);
/ / do
/ / {
/ 197 / index -= probe;
/ 198 / if (index < 0) {
/ 199 / index += length;
/ / }
**/ 201 / } while ((states[index] != 0) && ((states[index] == 2) || (set[index] != val)))**;
/ / }
/ /
/ /
/ 205 / return states[index] == 0 ? -1 : index;
/ / }

2. at gnu.trove.TIntHash.insertionIndex(TIntHash. java:271)

protected int insertionIndex(int val)
/ / {
byte[] states = _states;
/ 220 / int[] set = _set;
/ 221 / int length = states.length;
/ 222 / int hash = _hashingStrategy.computeHashCode(val) & 0x7FFFFFFF;
/ 223 / int index = hash % length;
/ /
/ 225 / if (states[index] == 0)
/ 226 / return index;
/ 227 / if ((states[index] == 1) && (set[index] == val)) {
/ 228 / return -index - 1;
/ / }
/ /
/ 231 / int probe = 1 + hash % (length - 2);
/ 245 / if (states[index] != 2)
/ / {
/ / do
/ / {
/ 249 / index -= probe;
/ 250 / if (index < 0) {
/ 251 / index += length;
/ / }
/ 253 / } while ((states[index] == 1) && (set[index] != val));
/ / }
/ /
/ /
/ /
/ /
/ 259 / if (states[index] == 2) {
/ 260 / int firstRemoved = index;
/ 261 / while ((states[index] != 0) && ((states[index] == 2) || (set[index] != val)))
/ / {
/ 263 / index -= probe;
/ 264 / if (index < 0) {
/ 265 / index += length;
/ / }
/ / }
/ 268 / return states[index] == 1 ? -index - 1 : firstRemoved;
/ / }
/ /
/ **271 / return states[index] == 1 ? -index - 1 : index;
/ / }**

3. at gnu.trove.TPrimitiveHash.capacity(TPrimitiveHash.java:99)

/ / protected int capacity()
/ / {
**/ 99 / return _states.length;**
/ / }

As you see, these lines do not do any division.

So why are these divide by zero exceptions being thrown? What are the scenarios within the gnu.trove classes that can thrown these java.lang.ArithmeticException: divide by zero exceptions in the code?

0

There are 0 best solutions below