sevenzip icon indicating copy to clipboard operation
sevenzip copied to clipboard

readSubStreamsInfo() panic "runtime error: index out of range [N] with length N"

Open marktayl opened this issue 2 months ago • 3 comments

When id == idSize in readSubStreamsInfo(), you execute the loops:

        s.size = make([]uint64, files)
        k := 0

        for i := range s.streams {
            total := uint64(0)

            for j := uint64(1); j < s.streams[i]; j++ {
                if s.size[k], err = readUint64(r); err != nil {
                    return nil, err
                }

                total += s.size[k]
                k++
            }

            s.size[k] = folder[i].unpackSize() - total
            k++  // <--- SUSPECT
        }

        id, err = r.ReadByte()
        if err != nil {
            return nil, fmt.Errorf("readSubStreamsInfo: ReadByte error: %w", err)
        }

I have a sample 7z (I cannot give it to you) that has [25, 0, 0] files. This causes the s.size[k] to panic with the error "index out of range [25] with length 25", because it gets to k=25 when i==1 when processing the second folder, aka s.streams[1]. I believe that the k++ at the end of the outer loop, which I marked "SUSPECT", is where I believe the fault is - that increment is not required (proper k value incrementing is handled inside the for j loop).

marktayl avatar Oct 13 '25 13:10 marktayl

If I remove that latter k++ then existing tests start failing, so it's more nuanced than that.

bodgit avatar Oct 14 '25 00:10 bodgit

If you add the following at the beginning of the outer loop, does that fix things?

if s.streams[i] == 0 {
        continue
}

That looks like it should work and doesn't break any of the existing tests.

bodgit avatar Oct 14 '25 08:10 bodgit

Yup, it worked for the archive that originally showed the issue.

marktayl avatar Oct 14 '25 13:10 marktayl