Skip to content

Commit a6e47e5

Browse files
committed
Never try to copy more than block len
This fixed a regression which one was introduced at b7bd1f5 as bugfix for another issue. When it happened, it throw `ArrayIndexOutOfBoundsException` and doesn't produce incorrect hash.
1 parent 83c74ec commit a6e47e5

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
All notable changes to this project will be documented in this file.
44

55
## [unreleased]
6+
- Fixed regression on chunked data that was introduced in 2.6.0.
67

78
## [2.6.0] - 2021-05-17
89
- Fixed an issue when chunked data by block len produced incorrect block.

shared/src/main/scala/ky/korins/blake3/ChunkState.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,17 @@ private[blake3] class ChunkState(
7474

7575
val available = BLOCK_LEN - blockLen
7676
var consume = Math.min(available, to - i)
77-
if (consume < available) {
77+
if (consume > 0) {
7878
System.arraycopy(bytes, i, block, blockLen, consume)
7979
blockLen += consume
8080
i += consume
81-
compressIfRequired()
8281
}
8382

8483
consume = to - i
84+
if (consume > 0) {
85+
compressIfRequired()
86+
}
87+
8588
while (consume > BLOCK_LEN) {
8689
blockLen = BLOCK_LEN
8790
compressedWords(bytes, i)

shared/src/test/scala/ky/korins/blake3/Blake3Test.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,15 @@ class Blake3Test extends AnyWordSpec with should.Matchers {
254254
expected shouldBe actual
255255
}
256256
}
257+
258+
"Regression in 2.6.0" in {
259+
val bytes = new Array[Byte](146)
260+
Random.nextBytes(bytes)
261+
val expected = Blake3.base16(bytes, 32)
262+
val hasher = Blake3.newHasher()
263+
hasher.update(bytes, 0, 125)
264+
hasher.update(bytes, 125, 21)
265+
val actual = hasher.doneBase16(32)
266+
expected shouldBe actual
267+
}
257268
}

0 commit comments

Comments
 (0)