]> Cypherpunks repositories - gostls13.git/commitdiff
archive/zip: if non-zero base offset fails, fall back to zero
authorIan Lance Taylor <iant@golang.org>
Wed, 25 May 2022 21:12:29 +0000 (14:12 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 2 Jun 2022 16:25:34 +0000 (16:25 +0000)
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 <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/archive/zip/reader.go
src/archive/zip/reader_test.go
src/archive/zip/testdata/test-baddirsz.zip [new file with mode: 0644]

index da6d869db43e374f185673ccc305797048e99308..12b650990dd63d40e1e39910884d31c5add5183e 100644 (file)
@@ -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
                }
index 41e720aae7a82ac659ccea3cb4603d46d0f3d664..84742c7d2af3c78b98bb7f936fb249bd39eb6c4b 100644 (file)
@@ -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 (file)
index 0000000..45b3314
Binary files /dev/null and b/src/archive/zip/testdata/test-baddirsz.zip differ