Skip to content

Commit

Permalink
Fixes 53#issue-782891651 (java.lang.ArrayIndexOutOfBoundsException fr…
Browse files Browse the repository at this point in the history
…om StreamDeserializationContext.read() if Hash.from() is called for more than 1048576 bytes)
  • Loading branch information
gregoreficint committed Jan 10, 2021
1 parent 5ef5a27 commit fe98435
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public byte[] read(int l) {
return null;
}

if (l > this.buffer.length) {
l = this.buffer.length;
if (l+this.counter > this.buffer.length) {
l = this.buffer.length-this.counter;
}

// const uint8Array = new Uint8Array(this.buffer,this.counter,l);
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/com/eternitywall/ots/op/TestOps.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.eternitywall.ots.op;

import com.eternitywall.ots.DetachedTimestampFile;
import com.eternitywall.ots.Hash;
import com.eternitywall.ots.StreamDeserializationContext;
import com.eternitywall.ots.Utils;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;
Expand Down Expand Up @@ -85,4 +88,36 @@ public void test100M() throws Exception {
byte[] fileDigest = timestampFile.fileDigest();
assertArrayEquals(Utils.hexToBytes(hash), fileDigest);
}

// Test "Hash.from" below chunk size
@Test
public void hashFd1() throws Exception {
String hash = "CA7ED0C4A8E67CBDC461C4CB0D286D2FABBD9F0C41A7F42B665F72EBAA8AEC56";
int size = 1048576-1;
byte[] buffer = new byte[size];
Hash myHash=Hash.from(buffer, OpSHA256._TAG);
assertArrayEquals(Utils.hexToBytes(hash), myHash.getValue());
}

// Test "Hash.from" at edge case of being equal to chunk size
@Test
public void hashFd2() throws Exception {
String hash = "30E14955EBF1352266DC2FF8067E68104607E750ABB9D3B36582B8AF909FCB58";
int size = 1048576;
byte[] buffer = new byte[size];
Hash myHash=Hash.from(buffer, OpSHA256._TAG);
assertArrayEquals(Utils.hexToBytes(hash), myHash.getValue());
}

// Test "Hash.from" above chunk size
@Test
public void hashFd3() throws Exception {
String hash = "2CB74EDBA754A81D121C9DB6833704A8E7D417E5B13D1A19F4A52F007D644264";
int size = 1048576+1;
byte[] buffer = new byte[size];
Hash myHash=Hash.from(buffer, OpSHA256._TAG);
assertArrayEquals(Utils.hexToBytes(hash), myHash.getValue());
}


}

0 comments on commit fe98435

Please sign in to comment.