Skip to content

Commit

Permalink
Enforcing maximum size on BigInteger creation. See #497
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 15, 2023
1 parent f5013e3 commit e30ac12
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions convex-core/src/main/java/convex/core/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,7 @@ public class Constants {

public static final long INITIAL_PEER_TIMESTAMP = -1L;

public static final int MAX_BIG_INTEGER_LENGTH = 4096;


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigInteger;

import convex.core.Constants;
import convex.core.data.ABlob;
import convex.core.data.ACell;
import convex.core.data.AString;
Expand Down Expand Up @@ -29,6 +30,7 @@ public final class CVMBigInteger extends AInteger {
public static final BigInteger MIN_NEGATIVE_BIG=new BigInteger("-9223372036854775809");

protected static final long LONG_BYTELENGTH = 8;
protected static final long MAX_BYTELENGTH = Constants.MAX_BIG_INTEGER_LENGTH;

// We store the Integer as either a blob or Java BigInteger, and convert lazily on demand
private ABlob blob;
Expand All @@ -43,10 +45,11 @@ private CVMBigInteger(ABlob blob, BigInteger value) {
* Creates a CVMBigInteger
* WARNING: might not be canonical
* @param bs Bytes representing BigInteger value. Highest bit assumed to be sign.
* @return CVMBigInteger instance
* @return CVMBigInteger instance or null if not valid
*/
public static CVMBigInteger wrap(byte[] bs) {
byte[] tbs=Utils.trimBigIntegerLeadingBytes(bs);
if (tbs.length>MAX_BYTELENGTH) return null;
if (tbs==bs) tbs=tbs.clone(); // Defensive copy just in case
return new CVMBigInteger(Blob.wrap(tbs),null);
}
Expand All @@ -55,9 +58,10 @@ public static CVMBigInteger wrap(byte[] bs) {
* Creates a CVMBigInteger
* WARNING: might not be canonical
* @param value Java BigInteger
* @return CVMBigInteger instance
* @return CVMBigInteger instance or null if not valid
*/
public static CVMBigInteger wrap(BigInteger value) {
if (value.bitLength()>(MAX_BYTELENGTH*8-1)) return null; // note bitLength excludes sign bit
return new CVMBigInteger(null,value);
}

Expand Down Expand Up @@ -321,7 +325,9 @@ public AInteger sub(AInteger b) {
public AInteger negate() {
BigInteger bi=big();
bi=bi.negate();
return CVMBigInteger.wrap(bi).toCanonical();
AInteger neg=CVMBigInteger.wrap(bi);
if (neg==null) return null; // can theoretically overflow....
return neg.toCanonical();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -13,6 +14,7 @@
import org.junit.jupiter.api.Test;

import convex.core.Constants;
import convex.core.data.ABlob;
import convex.core.data.AString;
import convex.core.data.Blob;
import convex.core.data.Blobs;
Expand Down Expand Up @@ -89,6 +91,22 @@ public class BigIntegerTest {
ObjectsTest.doAnyValueTests(cb);
}

@Test
public void testMaxSize() {
byte [] bs=new byte[Constants.MAX_BIG_INTEGER_LENGTH];
bs[0]=-128; // set sign bit for max sized negative number
ABlob blob=Blob.wrap(bs);
CVMBigInteger b=CVMBigInteger.create(blob);
assertTrue(b.isCanonical());
assertEquals(Constants.MAX_BIG_INTEGER_LENGTH,b.blob().count());
assertNull(b.negate());
assertNull(b.dec()); // overflow
assertNull(b.multiply(CVMLong.create(2))); // overflow
assertNotNull(b.inc().negate());

doBigTest(b);
}

@Test
public void testByteArrayConstruction() {
byte[] bs=new byte[] {-1,-1,-1,-1,-1,-1,-1,-1,-128};
Expand Down

0 comments on commit e30ac12

Please sign in to comment.