Skip to content

Commit 53c1da1

Browse files
committed
More adversarial data tests for Index
1 parent 0306e4e commit 53c1da1

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

convex-core/src/main/java/convex/core/data/Address.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public final class Address extends ABlobLike<CVMLong> {
4141
public static final Address MAX_VALUE = Address.create(Long.MAX_VALUE);
4242

4343
/**
44-
* Length of an Address in bytes (considered as a Blob)
44+
* Length of an Address in bytes (when considered as a Blob)
4545
*/
4646
static final int BYTE_LENGTH = 8;
4747

@@ -67,6 +67,16 @@ public static Address create(long number) {
6767
}
6868
return new Address(number);
6969
}
70+
71+
/**
72+
* Creates an Address without checking.
73+
*
74+
* @param number Account number
75+
* @return Address instance, may be invalid
76+
*/
77+
public static Address unsafeCreate(long number) {
78+
return new Address(number);
79+
}
7080

7181
/**
7282
* Obtains an Address from a blob. Must be a valid long value

convex-core/src/main/java/convex/core/data/Index.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,10 @@ public static <K extends ABlobLike, V extends ACell> Index<K, V> read(Blob b, in
593593
Index<K,V> result;
594594
long depth = Format.readVLCCount(b,epos);
595595
if (depth < 0) throw new BadFormatException("Negative depth!");
596+
if (depth >=MAX_DEPTH) {
597+
if (depth==MAX_DEPTH) throw new BadFormatException("More than one entry and MAX_DEPTH");
598+
throw new BadFormatException("Excessive depth!");
599+
}
596600
epos+=Format.getVLCCountLength(depth);
597601

598602
// Need to include children

convex-core/src/test/java/convex/core/data/AdversarialDataTest.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public class AdversarialDataTest {
7878
invalidTest(MapLeaf.unsafeCreate(new MapEntry[0]));
7979
}
8080

81+
@Test public void testBadAddress() {
82+
invalidTest(Address.unsafeCreate(-1));
83+
invalidTest(Address.unsafeCreate(Long.MIN_VALUE));
84+
}
85+
8186
@SuppressWarnings({ "unchecked", "null" })
8287
@Test public void testBadSetTree() {
8388
SetTree<CVMLong> a = Samples.INT_SET_300;
@@ -204,6 +209,19 @@ public void testBadIndex() {
204209
Index c2=Index.create(Blobs.fromHex("1231"),CVMLong.ZERO);
205210
invalidTest(Index.unsafeCreate(3, null, new Ref[] {c1.getRef(),c2.getRef()}, 5, 2));
206211
}
212+
213+
{ // Two colliding children
214+
Index c1=Index.create(Blobs.fromHex("1230"),CVMLong.ONE);
215+
Index c2=Index.create(Blobs.fromHex("1230"),CVMLong.ZERO);
216+
invalidTest(Index.unsafeCreate(3, null, new Ref[] {c1.getRef(),c2.getRef()}, 1, 2));
217+
invalidTest(Index.unsafeCreate(3, null, new Ref[] {c1.getRef(),c2.getRef()}, 3, 2));
218+
}
219+
220+
{ // Two colliding children at max depth
221+
Index c1=Index.create(Blobs.fromHex("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef00"),CVMLong.ONE);
222+
Index c2=Index.create(Blobs.fromHex("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef11"),CVMLong.ONE);
223+
invalidTest(Index.unsafeCreate(64, null, new Ref[] {c1.getRef(),c2.getRef()}, 3, 2));
224+
}
207225

208226
}
209227

@@ -303,7 +321,7 @@ private void doEncodingTest(ACell b) {
303321
Blob enc=null;
304322
try {
305323
enc= b.getEncoding();
306-
} catch (Throwable t) {
324+
} catch (Exception t) {
307325
// probably no valid encoding, so skip this test
308326
return;
309327
}
@@ -316,7 +334,7 @@ private void doEncodingTest(ACell b) {
316334
// not a readable format, so probably not dangerous
317335
return;
318336
} catch (InvalidDataException e) {
319-
fail("Failed to validate after re-reading?");
337+
fail("Failed to validate after re-reading?",e);
320338
}
321339

322340
if (c.isCompletelyEncoded()) {

0 commit comments

Comments
 (0)