From: Ian Lance Taylor Date: Wed, 25 May 2022 21:12:29 +0000 (-0700) Subject: archive/zip: if non-zero base offset fails, fall back to zero X-Git-Tag: go1.19beta1~89 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=f70b93a6e9ab6ef6ec4a9f1748f852e1601c0905;p=gostls13.git archive/zip: if non-zero base offset fails, fall back to zero This permits us to read files that earlier Go releases could read. It is also compatible with other zip programs. Change-Id: I7e2999f1073c4db5ba3f51f92681e0b149d55b3e Reviewed-on: https://go-review.googlesource.com/c/go/+/408734 Run-TryBot: Ian Lance Taylor Run-TryBot: Ian Lance Taylor TryBot-Result: Gopher Robot Auto-Submit: Ian Lance Taylor Reviewed-by: Michael Knyszek Reviewed-by: Ian Lance Taylor --- diff --git a/src/archive/zip/reader.go b/src/archive/zip/reader.go index da6d869db4..12b650990d 100644 --- a/src/archive/zip/reader.go +++ b/src/archive/zip/reader.go @@ -123,6 +123,20 @@ func (z *Reader) init(r io.ReaderAt, size int64) error { for { f := &File{zip: z, zipr: r} err = readDirectoryHeader(f, buf) + + // For compatibility with other zip programs, + // if we have a non-zero base offset and can't read + // the first directory header, try again with a zero + // base offset. + if err == ErrFormat && z.baseOffset != 0 && len(z.File) == 0 { + z.baseOffset = 0 + if _, err = rs.Seek(int64(end.directoryOffset), io.SeekStart); err != nil { + return err + } + buf = bufio.NewReader(rs) + continue + } + if err == ErrFormat || err == io.ErrUnexpectedEOF { break } diff --git a/src/archive/zip/reader_test.go b/src/archive/zip/reader_test.go index 41e720aae7..84742c7d2a 100644 --- a/src/archive/zip/reader_test.go +++ b/src/archive/zip/reader_test.go @@ -108,6 +108,24 @@ var tests = []ZipTest{ }, }, }, + { + Name: "test-baddirsz.zip", + Comment: "This is a zipfile comment.", + File: []ZipTestFile{ + { + Name: "test.txt", + Content: []byte("This is a test text file.\n"), + Modified: time.Date(2010, 9, 5, 12, 12, 1, 0, timeZone(+10*time.Hour)), + Mode: 0644, + }, + { + Name: "gophercolor16x16.png", + File: "gophercolor16x16.png", + Modified: time.Date(2010, 9, 5, 15, 52, 58, 0, timeZone(+10*time.Hour)), + Mode: 0644, + }, + }, + }, { Name: "r.zip", Source: returnRecursiveZip, diff --git a/src/archive/zip/testdata/test-baddirsz.zip b/src/archive/zip/testdata/test-baddirsz.zip new file mode 100644 index 0000000000..45b3314076 Binary files /dev/null and b/src/archive/zip/testdata/test-baddirsz.zip differ