Skip to content

Commit 48d865a

Browse files
ebfeadg
authored andcommitted
archive/zip: fix returned error on truncated data descriptor
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]>
1 parent 6a34206 commit 48d865a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/archive/zip/reader.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ func (r *checksumReader) Read(b []byte) (n int, err error) {
184184
}
185185
if r.desr != nil {
186186
if err1 := readDataDescriptor(r.desr, r.f); err1 != nil {
187-
err = err1
187+
if err1 == io.EOF {
188+
err = io.ErrUnexpectedEOF
189+
} else {
190+
err = err1
191+
}
188192
} else if r.hash.Sum32() != r.f.CRC32 {
189193
err = ErrChecksum
190194
}

src/archive/zip/reader_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -582,3 +582,26 @@ func TestIssue10956(t *testing.T) {
582582
t.Errorf("error = %v; want %q", err, want)
583583
}
584584
}
585+
586+
// Verify we return ErrUnexpectedEOF when reading truncated data descriptor.
587+
func TestIssue11146(t *testing.T) {
588+
data := []byte("PK\x03\x040000000000000000" +
589+
"000000\x01\x00\x00\x000\x01\x00\x00\xff\xff0000" +
590+
"0000000000000000PK\x01\x02" +
591+
"0000\b0\b\x00000000000000" +
592+
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x000000PK\x05\x06\x00\x00" +
593+
"\x00\x0000\x01\x0000008\x00\x00\x00\x00\x00")
594+
z, err := NewReader(bytes.NewReader(data), int64(len(data)))
595+
if err != nil {
596+
t.Fatal(err)
597+
}
598+
r, err := z.File[0].Open()
599+
if err != nil {
600+
t.Fatal(err)
601+
}
602+
_, err = ioutil.ReadAll(r)
603+
if err != io.ErrUnexpectedEOF {
604+
t.Errorf("File[0] error = %v; want io.ErrUnexpectedEOF", err)
605+
}
606+
r.Close()
607+
}

0 commit comments

Comments
 (0)