Skip to content

Commit

Permalink
archive/tar: skip NUL-filled unused octal fields
Browse files Browse the repository at this point in the history
Fixes #5290.

R=golang-dev, dave, bradfitz, r
CC=golang-dev
https://golang.org/cl/8763044
  • Loading branch information
minux committed May 14, 2013
1 parent 392cebe commit 9c94580
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/pkg/archive/tar/reader.go
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions src/pkg/archive/tar/reader_test.go
Expand Up @@ -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) {
Expand Down
Binary file added src/pkg/archive/tar/testdata/nil-uid.tar
Binary file not shown.

0 comments on commit 9c94580

Please sign in to comment.