@@ -309,7 +309,6 @@ private short loadFlags() {
309
309
return (short ) flags ;
310
310
}
311
311
312
- private static final long UNSIGNED_INT_MASK = (1L << Integer .SIZE ) - 1L ;
313
312
private static final BigInteger UNSIGNED_LONG_MASK = BigInteger .ONE .shiftLeft (Long .SIZE ).subtract (BigInteger .ONE );
314
313
315
314
private Object loadInteger () {
@@ -323,33 +322,41 @@ private Object loadInteger() {
323
322
int firstWord = loadVarUInt ();
324
323
if (wordsLength == 1 ) {
325
324
if (firstWord < 0 ) {
326
- long words = firstWord & UNSIGNED_INT_MASK ;
325
+ if (negative && firstWord == Integer .MIN_VALUE ) {
326
+ return Integer .MIN_VALUE ;
327
+ }
328
+
329
+ long words = Integer .toUnsignedLong (firstWord );
327
330
return negative ? -words : words ;
328
331
}
329
332
return negative ? -firstWord : firstWord ;
330
333
}
331
334
332
335
// Load the second word. If there are only two words, then return a long
333
- // if it fits into one and a BigInt otherwise.
336
+ // if it fits into one and a BigInteger otherwise.
334
337
int secondWord = loadVarUInt ();
335
338
if (wordsLength == 2 ) {
336
- long words = ((long ) secondWord << 32L ) | (firstWord & UNSIGNED_INT_MASK );
339
+ long words = ((( long ) secondWord ) << 32L ) | Integer . toUnsignedLong (firstWord );
337
340
if (words < 0L ) {
341
+ if (negative && words == Long .MIN_VALUE ) {
342
+ return Long .MIN_VALUE ;
343
+ }
344
+
338
345
BigInteger result = BigInteger .valueOf (words ).and (UNSIGNED_LONG_MASK );
339
346
return negative ? result .negate () : result ;
340
347
}
341
348
return negative ? -words : words ;
342
349
}
343
350
344
351
// Otherwise, load the remaining words and return a BigInt.
345
- BigInteger result = BigInteger .valueOf (firstWord & UNSIGNED_INT_MASK );
346
- result = result .or (BigInteger .valueOf (secondWord & UNSIGNED_INT_MASK ).shiftLeft (32 ));
352
+ BigInteger result = BigInteger .valueOf (Integer . toUnsignedLong ( firstWord ) );
353
+ result = result .or (BigInteger .valueOf (Integer . toUnsignedLong ( secondWord ) ).shiftLeft (32 ));
347
354
348
355
for (int wordsIndex = 2 ; wordsIndex < wordsLength ; wordsIndex ++) {
349
- result = result .or (BigInteger .valueOf (loadVarUInt () & UNSIGNED_INT_MASK ).shiftLeft (wordsIndex * 32 ));
356
+ result = result .or (BigInteger .valueOf (Integer . toUnsignedLong ( loadVarUInt ()) ).shiftLeft (wordsIndex * 32 ));
350
357
}
351
358
352
- return result ;
359
+ return negative ? result . negate () : result ;
353
360
}
354
361
355
362
private Nodes .Node loadNode () {
0 commit comments