Skip to content

Commit

Permalink
Create basic fuzzing harness for proto decoder (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradleyjkemp authored Jul 17, 2019
1 parent ef5558f commit 7ebe72c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
18 changes: 18 additions & 0 deletions internal/proto_decoder/fuzz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package proto_decoder

import (
"github.com/bradleyjkemp/grpc-tools/internal"
)

func Fuzz(data []byte) int {
dec := NewDecoder()

_, err := dec.Decode("", &internal.Message{
RawMessage: data,
})
if err != nil {
return 0
}

return 1
}
8 changes: 6 additions & 2 deletions internal/proto_decoder/unknown_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ func (u *unknownFieldResolver) enrichMessage(descriptor *builder.MessageBuilder,
return err
}
field := builder.NewField(generatedFieldName, fieldType)
field.SetNumber(fieldNum)
if err := field.TrySetNumber(fieldNum); err != nil {
return err
}
field.SetJsonName(fmt.Sprintf("%d", fieldNum))
if len(unknownFieldContents) > 1 {
field.SetRepeated()
Expand Down Expand Up @@ -115,7 +117,9 @@ func (u *unknownFieldResolver) detectFieldType(fieldName string, fields []dynami

// probably is an embedded message
descriptor, _ := builder.FromMessage(dyn.GetMessageDescriptor())
descriptor.SetName(fieldName)
if err := descriptor.TrySetName(fieldName); err != nil {
return nil, err
}
err = u.enrichMessage(descriptor, dyn)
if err != nil {
return nil, err
Expand Down

0 comments on commit 7ebe72c

Please sign in to comment.