Skip to content

Commit 571ca58

Browse files
committed
1 parent cbbc951 commit 571ca58

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/yarp/java/org/prism/Loader.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ private short loadFlags() {
309309
return (short) flags;
310310
}
311311

312-
private static final long UNSIGNED_INT_MASK = (1L << Integer.SIZE) - 1L;
313312
private static final BigInteger UNSIGNED_LONG_MASK = BigInteger.ONE.shiftLeft(Long.SIZE).subtract(BigInteger.ONE);
314313

315314
private Object loadInteger() {
@@ -323,33 +322,41 @@ private Object loadInteger() {
323322
int firstWord = loadVarUInt();
324323
if (wordsLength == 1) {
325324
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);
327330
return negative ? -words : words;
328331
}
329332
return negative ? -firstWord : firstWord;
330333
}
331334

332335
// 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.
334337
int secondWord = loadVarUInt();
335338
if (wordsLength == 2) {
336-
long words = ((long) secondWord << 32L) | (firstWord & UNSIGNED_INT_MASK);
339+
long words = (((long) secondWord) << 32L) | Integer.toUnsignedLong(firstWord);
337340
if (words < 0L) {
341+
if (negative && words == Long.MIN_VALUE) {
342+
return Long.MIN_VALUE;
343+
}
344+
338345
BigInteger result = BigInteger.valueOf(words).and(UNSIGNED_LONG_MASK);
339346
return negative ? result.negate() : result;
340347
}
341348
return negative ? -words : words;
342349
}
343350

344351
// 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));
347354

348355
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));
350357
}
351358

352-
return result;
359+
return negative ? result.negate() : result;
353360
}
354361

355362
private Nodes.Node loadNode() {

0 commit comments

Comments
 (0)