Repository: commons-compress Updated Branches: refs/heads/COMPRESS-380 87a3cfaf4 -> 07ed54502
COMPRESS-380 -1 is certainly a legal value for a byte Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/07ed5450 Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/07ed5450 Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/07ed5450 Branch: refs/heads/COMPRESS-380 Commit: 07ed545022f30604d0a4fc4ad5e4866e24e6a172 Parents: 87a3cfa Author: Stefan Bodewig <bode...@apache.org> Authored: Sun Jan 7 10:52:16 2018 +0100 Committer: Stefan Bodewig <bode...@apache.org> Committed: Sun Jan 7 10:52:16 2018 +0100 ---------------------------------------------------------------------- .../compress/compressors/deflate64/HuffmanDecoder.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/07ed5450/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java b/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java index f74c7de..9d6585a 100644 --- a/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java +++ b/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java @@ -445,6 +445,7 @@ class HuffmanDecoder implements Closeable { private final byte[] memory; private final int mask; private int wHead; + private boolean wrappedAround; private DecodingMemory() { this(16); @@ -452,7 +453,6 @@ class HuffmanDecoder implements Closeable { private DecodingMemory(int bits) { memory = new byte[1 << bits]; - Arrays.fill(memory, (byte) -1); mask = memory.length - 1; } @@ -467,7 +467,7 @@ class HuffmanDecoder implements Closeable { throw new IllegalStateException("Illegal distance parameter: " + distance); } int start = (wHead - distance) & mask; - if (memory[start] == -1) { + if (!wrappedAround && start >= wHead) { throw new IllegalStateException("Attempt to read beyond memory: dist=" + distance); } for (int i = 0, pos = start; i < length; i++, pos = incCounter(pos)) { @@ -476,7 +476,11 @@ class HuffmanDecoder implements Closeable { } private int incCounter(int counter) { - return (counter + 1) & mask; + final int newCounter = (counter + 1) & mask; + if (!wrappedAround && newCounter < counter) { + wrappedAround = true; + } + return newCounter; } }