Skip to content

Commit

Permalink
Handle empty buffer (#1)
Browse files Browse the repository at this point in the history
* data/rawv2 tests: Typo less & format harder

* data/rawv2 tests: Panicking test for empty payload

* data/rawv2: Do not panic when given an empty buffer to parse
  • Loading branch information
susji committed Apr 3, 2024
1 parent 1f44ec5 commit 4431e51
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions data/rawv2/rawv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ func Parse(d []byte) (*RuuviRawV2, error) {
// ParseWithTime is as [Parse] except it permits passing an arbitrary timestamp
// to be included in the returned struct.
func ParseWithTime(d []byte, t time.Time) (*RuuviRawV2, error) {
if len(d) < 1 {
return nil, ErrorPacketTooSmall
}
minlen := 0
switch d[0] {
case data.VERSION_RAWV2:
Expand Down
8 changes: 7 additions & 1 deletion data/rawv2/rawv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func TestShort(t *testing.T) {
}
}

func TestBadVersio(t *testing.T) {
func TestBadVersion(t *testing.T) {
vector := fmt.Sprintf("%02X", data.VERSION_RAWV1) + VECTOR_GOOD[2:]
raw := make([]byte, len(vector)/2)
n, _ := hex.Decode(raw, []byte(vector))
Expand All @@ -212,5 +212,11 @@ func TestBadVersio(t *testing.T) {
if !errors.Is(err, rawv2.ErrorPacketNotV2) {
t.Fatal(err)
}
}

func TestEmptyPayload(t *testing.T) {
_, err := rawv2.Parse(make([]byte, 0))
if !errors.Is(err, rawv2.ErrorPacketTooSmall) {
t.Fatal(err)
}
}

0 comments on commit 4431e51

Please sign in to comment.