Skip to content

Commit

Permalink
fix: Handle panic when unpack info is missing (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
bodgit committed Nov 6, 2023
1 parent fbb30a6 commit db3ba77
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package sevenzip

var ErrMissingUnpackInfo = errMissingUnpackInfo
10 changes: 9 additions & 1 deletion reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestOpenReader(t *testing.T) {
tables := []struct {
name, file string
volumes []string
err error
}{
{
name: "no header compression",
Expand Down Expand Up @@ -140,6 +141,11 @@ func TestOpenReader(t *testing.T) {
name: "issue 87",
file: "issue87.7z",
},
{
name: "issue 113",
file: "COMPRESS-492.7z",
err: sevenzip.ErrMissingUnpackInfo,
},
}

for _, table := range tables {
Expand All @@ -149,7 +155,9 @@ func TestOpenReader(t *testing.T) {
t.Parallel()
r, err := sevenzip.OpenReader(filepath.Join("testdata", table.file))
if err != nil {
t.Fatal(err)
assert.ErrorIs(t, err, table.err)

return
}
defer r.Close()

Expand Down
Binary file added testdata/COMPRESS-492.7z
Binary file not shown.
9 changes: 7 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ const (
)

var (
errIncompleteRead = errors.New("sevenzip: incomplete read")
errUnexpectedID = errors.New("sevenzip: unexpected id")
errIncompleteRead = errors.New("sevenzip: incomplete read")
errUnexpectedID = errors.New("sevenzip: unexpected id")
errMissingUnpackInfo = errors.New("sevenzip: missing unpack info")
)

func readUint64(r io.ByteReader) (uint64, error) {
Expand Down Expand Up @@ -510,6 +511,10 @@ func readStreamsInfo(r util.Reader) (*streamsInfo, error) {
}

if id == idSubStreamsInfo {
if s.unpackInfo == nil {
return nil, errMissingUnpackInfo
}

if s.subStreamsInfo, err = readSubStreamsInfo(r, s.unpackInfo.folder); err != nil {
return nil, err
}
Expand Down

0 comments on commit db3ba77

Please sign in to comment.