From b01b63776e8e05b1d629b91a1e049840dd65e3e1 Mon Sep 17 00:00:00 2001 From: Jon Williams Date: Mon, 26 Aug 2024 14:37:39 -0400 Subject: [PATCH] fix: Avoid panic in Reader init (empty2.7z); header.filesInfo is nil. --- reader.go | 49 +++++++++++++++++++++++---------------------- reader_test.go | 4 ++++ testdata/empty2.7z | Bin 0 -> 93 bytes 3 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 testdata/empty2.7z diff --git a/reader.go b/reader.go index c3ebb5a..d413839 100644 --- a/reader.go +++ b/reader.go @@ -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) + 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) diff --git a/reader_test.go b/reader_test.go index 5b259d4..6169292 100644 --- a/reader_test.go +++ b/reader_test.go @@ -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", diff --git a/testdata/empty2.7z b/testdata/empty2.7z new file mode 100644 index 0000000000000000000000000000000000000000..801345d5c32cd02367893c8b161bc5fa9681f7b6 GIT binary patch literal 93 zcmXr7+Ou9=hJmHEg^N#?0Rj}Dw9hG~_dsT&G5b3HvvnQM#Uy@dzOuYF_sblC+#@2f npBTj17#TTb7}&WP85otB85vn)83Y&@c%--(xkOjGF)#oC5499W literal 0 HcmV?d00001