Martin Mirchev created IMAGING-391:
--------------------------------------

             Summary: Truncated TIFF strips decode without error: 
BitInputStream reads the end of the data as all-ones samples
                 Key: IMAGING-391
                 URL: https://issues.apache.org/jira/browse/IMAGING-391
             Project: Commons Imaging
          Issue Type: Bug
            Reporter: Martin Mirchev


The TIFF sample reader 
{{org.apache.commons.imaging.formats.tiff.datareaders.BitInputStream}} does not 
check for the end of the data. {{readBits(count)}} uses {{in.read()}} directly: 
for fields shorter than 8 bits it stores the -1 as the bit cache, so every 
following field reads as all ones, and for 8, 16, 24 and 32 bits it returns -1 
or ORs it into the value. A TIFF whose strip is shorter than the image needs 
therefore decodes without any error, with the missing pixels set to the maximum 
sample value (white for a gray image). For example, a 4x4 4-bit gray TIFF with 
an uncompressed strip truncated to its first byte decodes through 
{{Imaging.getBufferedImage}} with 14 of its 16 pixels at 255; the complete file 
decodes to the expected gradient.

Unit-level reproducer (same package):
{code:java}
BitInputStream bis = new BitInputStream(new ByteArrayInputStream(new byte[0]), 
ByteOrder.BIG_ENDIAN);
bis.readBits(4); // 15, expected an exception
bis.readBits(4); // 15
{code}

Suggested fix: throw an EOFException (an IOException, as the other errors of 
this class) when the underlying stream ends inside a bit field. 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)

Reply via email to