Skip to content

Commit

Permalink
Fixes timmathews#9 by introducing max value checking as done in githu…
Browse files Browse the repository at this point in the history
…b.com/canboat/canboat
  • Loading branch information
asbjorn committed Feb 21, 2022
1 parent a985633 commit 2bce74b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion nmea2k/raw_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,16 @@ func (msg *RawMessage) extractNumber(field *Field, start, end, offset, width uin
maxValue = 0x7FFFFFFFFFFFFFFF
}

if num > maxValue {
var reserved uint64
if maxValue >= 15 {
reserved = 2
} else if maxValue > 1 {
reserved = 1
} else {
reserved = 0
}

if num > maxValue-reserved {
e = &DecodeError{data, "Field not present"}
return
}
Expand Down
11 changes: 11 additions & 0 deletions nmea2k/raw_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,15 @@ func TestPgn127489NumberExtraction(t *testing.T) {
if ok {
t.Errorf("the value for the field 'total engine hours' is invalid: (%d). Need to check maximum value during extraction. RawMessage: %+v\n", engineHours, pgnParsed.Data[6])
}

// This RawMessage is the 'BAD' one with invalid values. Field 6 should be 'nil' with the proper validation.
msg = RawMessage{new(can.RawMessage)}
msg.Data = []byte{0x00, 0xb0, 0x09, 0xff, 0xff, 0xab, 0x7c, 0xff, 0x7f, 0x15, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x60, 0x02, 0xff, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x05}
msg.Pgn = uint32(127489)

pgnParsed = ParsePacket(msg.RawMessage)
alternator, ok := pgnParsed.Data[4].(float64)
if ok {
t.Errorf("the value for the field 'alternator_potential' is invalid: (%f). Need to check maximum value during extraction. RawMessage: %+v\n", alternator, pgnParsed.Data[4])
}
}

0 comments on commit 2bce74b

Please sign in to comment.