Skip to content

Commit 9b3f77a

Browse files
authored
Merge pull request #289 from carlosms/i-288
Fix panic for malformed UAST unmarshal
2 parents 69bd4ab + 3e4a3a8 commit 9b3f77a

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

server/service/uast.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ func UnmarshalUAST(data []byte) ([]*Node, error) {
6969
return nil, ErrUnmarshalUAST.New(err)
7070
}
7171

72+
if nodeLen < 1 {
73+
return nil, ErrUnmarshalUAST.New(fmt.Errorf("malformed data"))
74+
}
75+
7276
node := uast.NewNode()
7377
nodeBytes := buf.Next(int(nodeLen))
7478
if int32(len(nodeBytes)) != nodeLen {

server/service/uast_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package service_test
2+
3+
import (
4+
"bytes"
5+
"encoding/binary"
6+
"testing"
7+
8+
"github.com/src-d/gitbase-web/server/service"
9+
"github.com/stretchr/testify/suite"
10+
)
11+
12+
type UastSuite struct {
13+
suite.Suite
14+
}
15+
16+
func TestUastSuite(t *testing.T) {
17+
s := new(UastSuite)
18+
suite.Run(t, s)
19+
}
20+
21+
func (suite *UastSuite) TestNegativeNodeLen() {
22+
var nodeLen int32 = -20
23+
24+
buf := new(bytes.Buffer)
25+
err := binary.Write(buf, binary.BigEndian, nodeLen)
26+
suite.Require().NoError(err)
27+
28+
nodes, err := service.UnmarshalUAST(buf.Bytes())
29+
suite.Require().Error(err)
30+
suite.Require().Nil(nodes)
31+
}

0 commit comments

Comments
 (0)