Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go-fuzz fixes #151

Merged
merged 4 commits into from
Aug 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}