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

Prevent panic from malformed Mic-E #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 10 additions & 1 deletion packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ func (p *Packet) parse() error {
return err
}
p.Position = &pos
p.parseMicEData()
err = p.parseMicEData()
if err != nil {
return err
}

return nil // there is no additional data to parse
default:
Expand Down Expand Up @@ -295,6 +298,12 @@ func (p *Packet) parseMicEData() error {
var t string
for i := 0; i < 3; i++ {
mc := miceCodes[rune(p.Dst.Call[i])][1]

if len(mc) == 0 {
// Malformed Mic-E packet, prevent index out of bounds.
return ErrInvalidPacket
}

if strings.HasSuffix(mc, "(Custom)") {
t = messageTypeCustom
} else if strings.HasSuffix(mc, "(Std)") {
Expand Down
9 changes: 9 additions & 0 deletions packet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ func testDistance(a *Position, b *Position) float64 {
return 2 * earthRadius * math.Asin(math.Sqrt(h))
}

func TestBadPacket(t *testing.T) {
raw := "N0CALL-9>P_0_P?,N0CALL-3*,WIDE1*,N0CALL-13*,N0CALL-10*,WIDE2*:'{g+l >/Test"
_, err := ParsePacket(raw)

if err == nil {
t.Fatalf("Expected an error parsing a bad Mic-E packet.")
}
}

func TestPacket(t *testing.T) {
var tests = []struct {
Raw string
Expand Down