]> Cypherpunks repositories - gostls13.git/commitdiff
archive/tar: skip NUL-filled unused octal fields
authorShenghou Ma <minux.ma@gmail.com>
Tue, 14 May 2013 20:40:42 +0000 (04:40 +0800)
committerShenghou Ma <minux.ma@gmail.com>
Tue, 14 May 2013 20:40:42 +0000 (04:40 +0800)
Fixes #5290.

R=golang-dev, dave, bradfitz, r
CC=golang-dev
https://golang.org/cl/8763044

src/pkg/archive/tar/reader.go
src/pkg/archive/tar/reader_test.go
src/pkg/archive/tar/testdata/nil-uid.tar [new file with mode: 0644]

index 05f82a40dd9e4561dd66f0038abc4f103f2d31ec..c6c101507b1b8f960c2a6e9bcde38a44d635c2ec 100644 (file)
@@ -243,13 +243,15 @@ func (tr *Reader) octal(b []byte) int64 {
                return x
        }
 
-       // Removing leading spaces.
-       for len(b) > 0 && b[0] == ' ' {
-               b = b[1:]
-       }
-       // Removing trailing NULs and spaces.
-       for len(b) > 0 && (b[len(b)-1] == ' ' || b[len(b)-1] == '\x00') {
-               b = b[0 : len(b)-1]
+       // Because unused fields are filled with NULs, we need
+       // to skip leading NULs. Fields may also be padded with
+       // spaces or NULs.
+       // So we remove leading and trailing NULs and spaces to
+       // be sure.
+       b = bytes.Trim(b, " \x00")
+
+       if len(b) == 0 {
+               return 0
        }
        x, err := strconv.ParseUint(cString(b), 8, 64)
        if err != nil {
index 9a1968237138c2c974fd01e19e64cc801daef835..2cf3d717d5fcf011ec24d08acc9b67bc079824d2 100644 (file)
@@ -142,6 +142,25 @@ var untarTests = []*untarTest{
                        },
                },
        },
+       {
+               file: "testdata/nil-uid.tar", // golang.org/issue/5290
+               headers: []*Header{
+                       {
+                               Name:     "P1050238.JPG.log",
+                               Mode:     0664,
+                               Uid:      0,
+                               Gid:      0,
+                               Size:     14,
+                               ModTime:  time.Unix(1365454838, 0),
+                               Typeflag: TypeReg,
+                               Linkname: "",
+                               Uname:    "eyefi",
+                               Gname:    "eyefi",
+                               Devmajor: 0,
+                               Devminor: 0,
+                       },
+               },
+       },
 }
 
 func TestReader(t *testing.T) {
diff --git a/src/pkg/archive/tar/testdata/nil-uid.tar b/src/pkg/archive/tar/testdata/nil-uid.tar
new file mode 100644 (file)
index 0000000..cc9cfaa
Binary files /dev/null and b/src/pkg/archive/tar/testdata/nil-uid.tar differ