Martin Mirchev created IMAGING-390:
--------------------------------------
Summary: ZlibDeflate.decompress returns a partly filled array
without error for a truncated Deflate stream
Key: IMAGING-390
URL: https://issues.apache.org/jira/browse/IMAGING-390
Project: Commons Imaging
Issue Type: Bug
Reporter: Martin Mirchev
{{ZlibDeflate.decompress(byte[], int)}}, used for Deflate-compressed TIFF
strips and tiles, calls {{Inflater.inflate(result)}} once and returns
{{result}} without checking how many bytes were inflated. When the compressed
data is truncated, the method returns an array of the expected size whose tail
was never written (zeros), with no exception, so the image decodes with missing
data. The {{Inflater}} is also never {{end()}}ed, which leaves its native
memory to the finalizer.
Reproducer:
{code:java}
byte[] data = new byte[10_000];
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (i * 31 + i / 97);
}
byte[] compressed = ZlibDeflate.compress(data);
byte[] truncated = Arrays.copyOf(compressed, compressed.length / 2);
byte[] result = ZlibDeflate.decompress(truncated, data.length); // no
exception, length 10000, tail is zeros
{code}
PackBits, used for the same TIFF data, throws "Tiff: Unpack bits source
exhausted" in this situation. Suggested fix: inflate until the expected size is
reached, throw an ImagingException if the stream ends or runs out of input
first, and end the Inflater. A pull request with the fix and a test follows;
the full test suite passes with it.
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)