Martin Mirchev created COMPRESS-734:
---------------------------------------

             Summary: SevenZOutputFile ignores partial channel writes and 
writes a corrupt archive
                 Key: COMPRESS-734
                 URL: https://issues.apache.org/jira/browse/COMPRESS-734
             Project: Commons Compress
          Issue Type: Bug
            Reporter: Martin Mirchev


{{SevenZOutputFile}} ignores the return value of 
{{SeekableByteChannel.write(ByteBuffer)}} in its five write calls 
({{OutputStreamWrapper.write}} for small and large buffers, the single-byte 
{{write}}, the header, and the start header in {{finish()}}). The 
{{WritableByteChannel}} contract allows a channel to write fewer bytes than are 
remaining, but the CRC and {{fileBytesWritten}} still count all of them. With 
such a channel the archive is silently corrupt: {{close()}} succeeds, and 
reading the archive back fails.

Reproducer: a channel over a {{ByteArraySeekableByteChannel}} that accepts at 
most 1024 bytes per {{write}} call, one COPY entry of 100,100 bytes:
{code:java}
try (SevenZOutputFile out = new SevenZOutputFile(partialChannel)) {
    out.setContentCompression(SevenZMethod.COPY);
    final SevenZArchiveEntry entry = new SevenZArchiveEntry();
    entry.setName("data.bin");
    out.putArchiveEntry(entry);
    out.write(new byte[100_000]);
    out.write(new byte[100]);
    out.closeArchiveEntry();
} // no exception
SevenZFile.builder().setByteArray(output.toByteArray()).get();
// ArchiveException: 7z archive: packSize (100,100) is out of range
{code}

The ZIP writer already handles this with {{ZipIoUtil.writeAll}}, which loops 
until the buffer is written. Suggested fix: do the same in 
{{SevenZOutputFile}}. 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