From f70b93a6e9ab6ef6ec4a9f1748f852e1601c0905 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 25 May 2022 14:12:29 -0700 Subject: [PATCH] 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 --- src/archive/zip/reader.go | 14 ++++++++++++++ src/archive/zip/reader_test.go | 18 ++++++++++++++++++ src/archive/zip/testdata/test-baddirsz.zip | Bin 0 -> 1170 bytes 3 files changed, 32 insertions(+) create mode 100644 src/archive/zip/testdata/test-baddirsz.zip 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 0000000000000000000000000000000000000000..45b331407624d02ac41e3b53283e4960a6e9f832 GIT binary patch literal 1170 zcmWIWW@Zs#U|`^2XiQYKJ#hW)VM!oQ3M?YSP?B0)qE}K;5*otEz+CvJ$)^m6ODnh; z7+JnDGBAL3a-Te*6UMN}rFGJkM?wmLfr}l&aaaM)^pwV1FgBTd*)~VY5GrSri z$jrb1!XgYZ4C(m=8L36d`8oMThGrFpW_ksA>0oQD44Qqcff&u2&Hz7mUM?w+fxMm` zE&U=x?Zy@V2qPe0vcxr_Bsf2F!C6nVlf&(QE?5{rm?pSGb*exy9+#I&RurKk- z+m)0yRCPG=d-HHDZeo&8l5*8w*j(kQ)h25CKV`;*MHBS|=I94cI>i2RTbstM*llm) zO8IWizic_XckQR<_w%av+wcEed*5>UK?&z&H{O(96e0boH{+WCE(bu95f2^3EZ4O=VNZ?3)UvtyJg5QDRcf$^EI~Jki zWtQzJsL_x*GnsW)scd27+nNv&O9`(nP~TLZC!C)&dpmb9Gex+~DL%5m{kC!2{41u2r}!o#L;7=ONQ zE-qq{y7hnokNZ9E^>G%L>tA=N)+ZKnN{AiY63=$`mQ27(iv*7buak|7iv?tIZF@fl zt>|OAAsR5{#f{tFon;*@eKst4U%}L6{!DE$Q;-Gc;z`Q`4>jq0{kyW^?3@=d0sB55 zxoLcI^%PC5#tAeEd>m$^DR^;2N$vDP24)a*LyE?_;6T$0ZaWgUlR!) zO)1Y`?`E^OggslIl9kNpY|u0@YV$J_L(}Dqa{cO(OH|YIR<91PW~;8;CG9SvxT%P1 zf&1?r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