Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic when not filling up zipfs's read buffer #247

Merged
merged 2 commits into from
Jun 28, 2020
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
2 changes: 1 addition & 1 deletion zipfs/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (f *File) Read(p []byte) (n int, err error) {
}
err = f.fillBuffer(f.offset + int64(len(p)))
n = copy(p, f.buf[f.offset:])
f.offset += int64(len(p))
f.offset += int64(n)
return
}

Expand Down
43 changes: 43 additions & 0 deletions zipfs/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package zipfs

import (
"archive/zip"
"io"
"testing"
)

func TestFileRead(t *testing.T) {
zrc, err := zip.OpenReader("testdata/small.zip")
if err != nil {
t.Fatal(err)
}
zfs := New(&zrc.Reader)
f, err := zfs.Open("smallFile")
if err != nil {
t.Fatal(err)
}
info, err := f.Stat()
if err != nil {
t.Fatal(err)
}
chunkSize := info.Size() * 2 // read with extra large buffer

buf := make([]byte, chunkSize)
n, err := f.Read(buf)
if err != io.EOF {
t.Fatal("Failed to read file to completion:", err)
}
if n != int(info.Size()) {
t.Errorf("Expected read length to be %d, found: %d", info.Size(), n)
}

// read a second time to check f.offset and f.buf are correct
buf = make([]byte, chunkSize)
n, err = f.Read(buf)
if err != io.EOF {
t.Fatal("Failed to read a fully read file:", err)
}
if n != 0 {
t.Errorf("Expected read length to be 0, found: %d", n)
}
}
Binary file added zipfs/testdata/small.zip
Binary file not shown.