Skip to content

Commit

Permalink
Merge pull request #247 from JohnStarich/bugfix/zipfs-read-small-file
Browse files Browse the repository at this point in the history
Fix panic when not filling up zipfs's read buffer
  • Loading branch information
0xmichalis committed Jun 28, 2020
2 parents 1524d0a + e76d1f3 commit 64b7ddd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion zipfs/file.go
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
@@ -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.

0 comments on commit 64b7ddd

Please sign in to comment.