Skip to content

Commit

Permalink
Merge pull request #289 from carlosms/i-288
Browse files Browse the repository at this point in the history
Fix panic for malformed UAST unmarshal
  • Loading branch information
carlosms authored Nov 23, 2018
2 parents 69bd4ab + 3e4a3a8 commit 9b3f77a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions server/service/uast.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func UnmarshalUAST(data []byte) ([]*Node, error) {
return nil, ErrUnmarshalUAST.New(err)
}

if nodeLen < 1 {
return nil, ErrUnmarshalUAST.New(fmt.Errorf("malformed data"))
}

node := uast.NewNode()
nodeBytes := buf.Next(int(nodeLen))
if int32(len(nodeBytes)) != nodeLen {
Expand Down
31 changes: 31 additions & 0 deletions server/service/uast_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package service_test

import (
"bytes"
"encoding/binary"
"testing"

"github.com/src-d/gitbase-web/server/service"
"github.com/stretchr/testify/suite"
)

type UastSuite struct {
suite.Suite
}

func TestUastSuite(t *testing.T) {
s := new(UastSuite)
suite.Run(t, s)
}

func (suite *UastSuite) TestNegativeNodeLen() {
var nodeLen int32 = -20

buf := new(bytes.Buffer)
err := binary.Write(buf, binary.BigEndian, nodeLen)
suite.Require().NoError(err)

nodes, err := service.UnmarshalUAST(buf.Bytes())
suite.Require().Error(err)
suite.Require().Nil(nodes)
}

0 comments on commit 9b3f77a

Please sign in to comment.