Martin Mirchev created COMPRESS-737:
---------------------------------------

             Summary: SevenZFile rejects a valid archive mixing single- and 
multi-entry folders with folder CRCs (Badly terminated SubStreamsInfo)
                 Key: COMPRESS-737
                 URL: https://issues.apache.org/jira/browse/COMPRESS-737
             Project: Commons Compress
          Issue Type: Bug
            Reporter: Martin Mirchev


{{SevenZFile}} rejects a valid archive whose folders mix single-entry folders 
that have a folder CRC with multi-entry folders, depending on their order. For 
three COPY folders laid out as [1 entry + folder CRC], [2 entries, no folder 
CRC], [1 entry + folder CRC], opening fails with
{noformat}
ArchiveException: 7z archive: Badly terminated SubStreamsInfo
{noformat}
The same archive with the folders in the order [1 + CRC], [1 + CRC], [2] opens. 
7-Zip ({{7z t}}) reports both archives as OK and lists all four files.

The cause is the sanity check {{sanityCheckSubStreamsInfo}}, which counts the 
digests that SubStreamsInfo carries:
{code:java}
int folderIdx = 0;
for (final int numUnpackSubStreams : numUnpackSubStreamsPerFolder) {
    if (numUnpackSubStreams != 1 || stats.folderHasCrc == null || 
!stats.folderHasCrc.get(folderIdx++)) {
{code}
{{folderIdx}} only advances when the first two conditions are false, so after a 
folder with more than one substream every later folder is checked against the 
CRC flag of the folder before it, the digest count is wrong, and the check 
reads the wrong number of bytes. {{readSubStreamsInfo}}, which reads the header 
afterwards, uses each folder's own {{hasCrc}} and is correct.

Suggested fix: advance {{folderIdx}} once per folder. A pull request with the 
fix, the 141-byte archive as a test resource 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