Skip to content

Commit

Permalink
Merge pull request src-d#921 from jfontan/fix/empty-headers
Browse files Browse the repository at this point in the history
plumbing/object: fix panic when reading object header
  • Loading branch information
mcuadros committed Aug 21, 2018
2 parents 1cef577 + 1666236 commit 8120de8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
16 changes: 11 additions & 5 deletions plumbing/object/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,17 +199,23 @@ func (c *Commit) Decode(o plumbing.EncodedObject) (err error) {
}

split := bytes.SplitN(line, []byte{' '}, 2)

var data []byte
if len(split) == 2 {
data = split[1]
}

switch string(split[0]) {
case "tree":
c.TreeHash = plumbing.NewHash(string(split[1]))
c.TreeHash = plumbing.NewHash(string(data))
case "parent":
c.ParentHashes = append(c.ParentHashes, plumbing.NewHash(string(split[1])))
c.ParentHashes = append(c.ParentHashes, plumbing.NewHash(string(data)))
case "author":
c.Author.Decode(split[1])
c.Author.Decode(data)
case "committer":
c.Committer.Decode(split[1])
c.Committer.Decode(data)
case headerpgp:
c.PGPSignature += string(split[1]) + "\n"
c.PGPSignature += string(data) + "\n"
pgpsig = true
}
} else {
Expand Down
34 changes: 34 additions & 0 deletions plumbing/object/commit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,22 @@ RUysgqjcpT8+iQM1PblGfHR4XAhuOqN5Fx06PSaFZhqvWFezJ28/CLyX5q+oIVk=
c.Assert(err, IsNil)
c.Assert(decoded.PGPSignature, Equals, pgpsignature)

// signature with extra empty line, it caused "index out of range" when
// parsing it

pgpsignature2 := "\n" + pgpsignature

commit.PGPSignature = pgpsignature2
encoded = &plumbing.MemoryObject{}
decoded = &Commit{}

err = commit.Encode(encoded)
c.Assert(err, IsNil)

err = decoded.Decode(encoded)
c.Assert(err, IsNil)
c.Assert(decoded.PGPSignature, Equals, pgpsignature2)

// signature in author name

commit.PGPSignature = ""
Expand Down Expand Up @@ -461,3 +477,21 @@ func (s *SuiteCommit) TestPatchCancel(c *C) {
c.Assert(err, ErrorMatches, "operation canceled")

}

func (s *SuiteCommit) TestMalformedHeader(c *C) {
encoded := &plumbing.MemoryObject{}
decoded := &Commit{}
commit := *s.Commit

commit.PGPSignature = "\n"
commit.Author.Name = "\n"
commit.Author.Email = "\n"
commit.Committer.Name = "\n"
commit.Committer.Email = "\n"

err := commit.Encode(encoded)
c.Assert(err, IsNil)

err = decoded.Decode(encoded)
c.Assert(err, IsNil)
}

0 comments on commit 8120de8

Please sign in to comment.