Skip to content

Commit 38e5128

Browse files
committed
archive/zip: handle zip files with more than 65535 files
R=r CC=golang-dev https://golang.org/cl/4812048
1 parent 226fb09 commit 38e5128

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

src/pkg/archive/zip/reader.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,33 @@ func (z *Reader) init(r io.ReaderAt, size int64) os.Error {
8080
return err
8181
}
8282
z.r = r
83-
z.File = make([]*File, end.directoryRecords)
83+
z.File = make([]*File, 0, end.directoryRecords)
8484
z.Comment = end.comment
8585
rs := io.NewSectionReader(r, 0, size)
8686
if _, err = rs.Seek(int64(end.directoryOffset), os.SEEK_SET); err != nil {
8787
return err
8888
}
8989
buf := bufio.NewReader(rs)
90-
for i := range z.File {
91-
z.File[i] = &File{zipr: r, zipsize: size}
92-
if err := readDirectoryHeader(z.File[i], buf); err != nil {
90+
91+
// The count of files inside a zip is truncated to fit in a uint16.
92+
// Gloss over this by reading headers until we encounter
93+
// a bad one, and then only report a FormatError if
94+
// the file count modulo 65536 is incorrect.
95+
for {
96+
f := &File{zipr: r, zipsize: size}
97+
err := readDirectoryHeader(f, buf)
98+
if err == FormatError {
99+
break
100+
}
101+
if err != nil {
93102
return err
94103
}
104+
z.File = append(z.File, f)
95105
}
106+
if uint16(len(z.File)) != end.directoryRecords {
107+
return FormatError
108+
}
109+
96110
return nil
97111
}
98112

0 commit comments

Comments
 (0)