Skip to content

Commit

Permalink
Merge pull request #151 from akrennmair/go-fuzz-fixes
Browse files Browse the repository at this point in the history
go-fuzz fixes
  • Loading branch information
Sean Treadway committed Aug 20, 2015
2 parents 1aedb1f + eeed29b commit 56eb08f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
16 changes: 16 additions & 0 deletions fuzz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// +build gofuzz
package amqp

import "bytes"

func Fuzz(data []byte) int {
r := reader{bytes.NewReader(data)}
frame, err := r.ReadFrame()
if err != nil {
if frame != nil {
panic("frame is not nil")
}
return 0
}
return 1
}
11 changes: 7 additions & 4 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package amqp
import (
"bytes"
"encoding/binary"
"errors"
"io"
"time"
)
Expand Down Expand Up @@ -66,7 +67,7 @@ func (me *reader) ReadFrame() (frame frame, err error) {

case frameBody:
if frame, err = me.parseBodyFrame(channel, size); err != nil {
return
return nil, err
}

case frameHeartbeat:
Expand All @@ -79,7 +80,7 @@ func (me *reader) ReadFrame() (frame frame, err error) {
}

if _, err = io.ReadFull(me.r, scratch[:1]); err != nil {
return
return nil, err
}

if scratch[0] != frameEnd {
Expand Down Expand Up @@ -425,19 +426,21 @@ func (me *reader) parseBodyFrame(channel uint16, size uint32) (frame frame, err
}

if _, err = io.ReadFull(me.r, bf.Body); err != nil {
return
return nil, err
}

return bf, nil
}

var errHeartbeatPayload = errors.New("Heartbeats should not have a payload")

func (me *reader) parseHeartbeatFrame(channel uint16, size uint32) (frame frame, err error) {
hf := &heartbeatFrame{
ChannelId: channel,
}

if size > 0 {
panic("Heartbeats should not have a payload")
return nil, errHeartbeatPayload
}

return hf, nil
Expand Down
22 changes: 22 additions & 0 deletions read_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package amqp

import (
"strings"
"testing"
)

func TestGoFuzzCrashers(t *testing.T) {
testData := []string{
"\b000000",
"\x02\x16\x10�[��\t\xbdui�" + "\x10\x01\x00\xff\xbf\xef\xbfサn\x99\x00\x10r",
"\x0300\x00\x00\x00\x040000",
}

for idx, testStr := range testData {
r := reader{strings.NewReader(testStr)}
frame, err := r.ReadFrame()
if err != nil && frame != nil {
t.Errorf("%d. frame is not nil: %#v err = %v", idx, frame, err)
}
}
}

0 comments on commit 56eb08f

Please sign in to comment.