Martin Mirchev created COMPRESS-735:
---------------------------------------

             Summary: SevenZOutputFile accepts entries after finish() and 
overwrites the first entry's data
                 Key: COMPRESS-735
                 URL: https://issues.apache.org/jira/browse/COMPRESS-735
             Project: Commons Compress
          Issue Type: Bug
            Reporter: Martin Mirchev


{{SevenZOutputFile.putArchiveEntry}} adds the entry to its list without 
checking whether {{finish()}} was already called, and {{write}} after 
{{finish()}} opens a new entry stream. A second {{finish()}} throws "This 
archive has already been finished", but adding and writing an entry after the 
first one is silently accepted. The data written then lands where the first 
entry's packed data is, while the header written by {{finish()}} still 
describes the first entry, so the archive is corrupt.

Reproducer (COPY method):
{code:java}
SevenZOutputFile o = new SevenZOutputFile(file);
o.setContentCompression(SevenZMethod.COPY);
o.putArchiveEntry(o.createArchiveEntry(new File("a.txt"), "a.txt"));
o.write("AAA".getBytes(StandardCharsets.UTF_8));
o.closeArchiveEntry();
o.finish();
o.putArchiveEntry(o.createArchiveEntry(new File("lost.txt"), "lost.txt")); // 
accepted
o.write("BBB".getBytes(StandardCharsets.UTF_8));                          // 
accepted
o.closeArchiveEntry();
o.close();                                                                // no 
exception
// reading the archive back now fails the checksum of a.txt, whose bytes were 
overwritten with "BBB"
{code}

{{ZipArchiveOutputStream}} rejects this ("Stream has already been finished"). 
Suggested fix: reject {{putArchiveEntry}} and writes once the archive is 
finished. 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