Skip to content

Commit

Permalink
archive/zip: fix returned error on truncated data descriptor
Browse files Browse the repository at this point in the history
Return io.ErrUnexpectedEOF instead of io.EOF when reading a truncated
data descriptor.

Fixes #11146.

Change-Id: Ia1905955165fd38af3c557d1fa1703ed8be893e2
Reviewed-on: https://go-review.googlesource.com/11070
Reviewed-by: Andrew Gerrand <[email protected]>
  • Loading branch information
ebfe authored and adg committed Jun 14, 2015
1 parent 6a34206 commit 48d865a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/archive/zip/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,11 @@ func (r *checksumReader) Read(b []byte) (n int, err error) {
}
if r.desr != nil {
if err1 := readDataDescriptor(r.desr, r.f); err1 != nil {
err = err1
if err1 == io.EOF {
err = io.ErrUnexpectedEOF
} else {
err = err1
}
} else if r.hash.Sum32() != r.f.CRC32 {
err = ErrChecksum
}
Expand Down
23 changes: 23 additions & 0 deletions src/archive/zip/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,3 +582,26 @@ func TestIssue10956(t *testing.T) {
t.Errorf("error = %v; want %q", err, want)
}
}

// Verify we return ErrUnexpectedEOF when reading truncated data descriptor.
func TestIssue11146(t *testing.T) {
data := []byte("PK\x03\x040000000000000000" +
"000000\x01\x00\x00\x000\x01\x00\x00\xff\xff0000" +
"0000000000000000PK\x01\x02" +
"0000\b0\b\x00000000000000" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x000000PK\x05\x06\x00\x00" +
"\x00\x0000\x01\x0000008\x00\x00\x00\x00\x00")
z, err := NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
t.Fatal(err)
}
r, err := z.File[0].Open()
if err != nil {
t.Fatal(err)
}
_, err = ioutil.ReadAll(r)
if err != io.ErrUnexpectedEOF {
t.Errorf("File[0] error = %v; want io.ErrUnexpectedEOF", err)
}
r.Close()
}

0 comments on commit 48d865a

Please sign in to comment.