Skip to content

Commit

Permalink
fix: Avoid panic in Reader init (empty2.7z); header.filesInfo is nil.
Browse files Browse the repository at this point in the history
  • Loading branch information
wizardishungry committed Aug 26, 2024
1 parent d263b66 commit b01b637
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
49 changes: 25 additions & 24 deletions reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,41 +420,42 @@ func (z *Reader) init(r io.ReaderAt, size int64) error {
z.si = header.streamsInfo

// spew.Dump(header)
filesPerStream := make(map[int]int, z.si.Folders())

folder, offset := 0, int64(0)
z.File = make([]*File, 0, len(header.filesInfo.file))
j := 0
if header.filesInfo != nil {
folder, offset := 0, int64(0)
z.File = make([]*File, 0, len(header.filesInfo.file))
j := 0

filesPerStream := make(map[int]int, z.si.Folders())
for _, fh := range header.filesInfo.file {
f := new(File)
f.zip = z
f.FileHeader = fh

for _, fh := range header.filesInfo.file {
f := new(File)
f.zip = z
f.FileHeader = fh
if f.FileHeader.FileInfo().IsDir() && !strings.HasSuffix(f.FileHeader.Name, "/") {
f.FileHeader.Name += "/"
}

if f.FileHeader.FileInfo().IsDir() && !strings.HasSuffix(f.FileHeader.Name, "/") {
f.FileHeader.Name += "/"
}
if !fh.isEmptyStream && !fh.isEmptyFile {
f.folder, _ = header.streamsInfo.FileFolderAndSize(j)

if !fh.isEmptyStream && !fh.isEmptyFile {
f.folder, _ = header.streamsInfo.FileFolderAndSize(j)
// Make an exported copy of the folder index
f.Stream = f.folder

// Make an exported copy of the folder index
f.Stream = f.folder
filesPerStream[f.folder]++

filesPerStream[f.folder]++
if f.folder != folder {
offset = 0
}

if f.folder != folder {
offset = 0
f.offset = offset
offset += int64(f.UncompressedSize)

Check failure on line 452 in reader.go

View workflow job for this annotation

GitHub Actions / Build and Test (1.21)

G115: integer overflow conversion uint64 -> int64 (gosec)

Check failure on line 452 in reader.go

View workflow job for this annotation

GitHub Actions / Build and Test (1.22)

G115: integer overflow conversion uint64 -> int64 (gosec)
folder = f.folder
j++
}

f.offset = offset
offset += int64(f.UncompressedSize)
folder = f.folder
j++
z.File = append(z.File, f)
}

z.File = append(z.File, f)
}

// spew.Dump(filesPerStream)
Expand Down
4 changes: 4 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ func TestOpenReader(t *testing.T) {
name: "empty streams and files",
file: "empty.7z",
},
{
name: "empty2",
file: "empty2.7z",
},
{
name: "bcj2",
file: "bcj2.7z",
Expand Down
Binary file added testdata/empty2.7z
Binary file not shown.

0 comments on commit b01b637

Please sign in to comment.