Martin Mirchev created COMPRESS-733:
---------------------------------------

             Summary: ExplodingInputStream copies a dictionary byte when the 
stream ends in the length code of a back reference
                 Key: COMPRESS-733
                 URL: https://issues.apache.org/jira/browse/COMPRESS-733
             Project: Commons Compress
          Issue Type: Bug
            Reporter: Martin Mirchev


When the data of an imploded (method 6) ZIP entry ends inside the length code 
of a back reference, {{ExplodingInputStream.fillBuffer()}} gets -1 from 
{{lengthTree.read(bits)}} and carries on: the length becomes 
{{minimumMatchLength - 1}} and one byte is copied from the dictionary, which is 
still empty, into the output. The literal and distance branches already stop at 
the end of the stream; the length branch does not, so a truncated entry decodes 
to an extra byte instead of ending.

Reproducer (same package, two 64-symbol Shannon-Fano trees, then a back 
reference whose length code is cut off):
{code:java}
final byte[] tree = { 0x03, (byte) 0xF5, (byte) 0xF5, (byte) 0xF5, (byte) 0xF5 
};
final byte[] data = new byte[2 * tree.length + 2];
System.arraycopy(tree, 0, data, 0, tree.length);
System.arraycopy(tree, 0, data, tree.length, tree.length);
data[2 * tree.length] = (byte) 0x86;
data[2 * tree.length + 1] = 0x00;
try (ExplodingInputStream in = new ExplodingInputStream(4096, 2, new 
ByteArrayInputStream(data))) {
    System.out.println(IOUtils.toByteArray(in).length); // 1, expected 0
}
{code}
The same happens through {{ZipArchiveInputStream}} for a method 6 entry with 
this payload. Cutting the stream off before the distance code instead gives 0 
bytes, as expected.

Suggested fix: return when {{lengthTree.read(bits)}} returns -1, as the other 
two branches do. A pull request with the fix and a test follows.

This issue was found by an LLM-Enhanced Static Analysis Engine and verified 
manually.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to