Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions internal/bra/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (rc *readCloser) Read(p []byte) (int, error) {
return 0, fmt.Errorf("bra: error buffering: %w", err)
}

if rc.buf.Len() == 0 { // Buffer is empty and read closer returned EOF; no more data available
return 0, io.EOF
}

if rc.buf.Len() < rc.conv.Size() {
rc.n = rc.buf.Len()
}
Expand Down
53 changes: 53 additions & 0 deletions reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"
"testing/fstest"
"testing/iotest"
"time"

"github.com/bodgit/sevenzip"
"github.com/bodgit/sevenzip/internal/util"
Expand Down Expand Up @@ -434,6 +435,58 @@ func TestFS(t *testing.T) {
}
}

// TestBraRead is a separate test as the example archive cannot be added to
// the existing test cases in TestOpenReader as it triggers an error in the
// LZMA library which is unrelated to the bug that it is a test case for.
// However extracting this one file is enough to demonstrate the bug and
// validate the fix so it's here as a separate test.
func TestBraRead(t *testing.T) {
t.Parallel()

eg := new(errgroup.Group)

//nolint:wrapcheck
eg.Go(func() (err error) {
r, err := sevenzip.OpenReader(filepath.Join("testdata", "pr472.7z"))
if err != nil {
return err
}

defer func() {
err = errors.Join(err, r.Close())
}()

file, err := r.Open("Plug 1.2/Plug 1.2.exe")
if err != nil {
return err
}

defer func() {
err = errors.Join(err, file.Close())
}()

if _, err = io.ReadAll(file); err != nil {
return err
}

return nil
})

done := make(chan error, 1)
go func() {
done <- eg.Wait()
}()

select {
case err := <-done:
if err != nil {
t.Fatal(err)
}
case <-time.After(2 * time.Second):
t.Fatalf("timeout waiting for reader to complete")
}
}

func FuzzNewReaderWithPassword(f *testing.F) {
b, err := os.ReadFile(filepath.Join("testdata", "copy.7z"))
if err != nil {
Expand Down
Binary file added testdata/pr472.7z
Binary file not shown.
Loading