Skip to content

Commit

Permalink
Fix ArrayedData WRT an empty binary
Browse files Browse the repository at this point in the history
  • Loading branch information
danielspofford committed Apr 10, 2019
1 parent ed29a3e commit b2ce507
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/harald/hci/arrayed_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ defmodule Harald.HCI.ArrayedData do
@doc """
Deserializes the binary representation of a list of structs according to `schema`.
"""
def deserialize(schema, length, struct_module, bin)

def deserialize(_, length, _, <<>>) when length > 0, do: {:error, :incomplete}

def deserialize(schema, length, struct_module, bin) do
schema
|> deserialize(bin, %{
Expand Down
12 changes: 12 additions & 0 deletions test/harald/hci/arrayed_data_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Harald.HCI.ArrayedDataTest do
use ExUnit.Case, async: true
alias Harald.HCI.{ArrayedData, Event.LEMeta.AdvertisingReport.Device}

doctest ArrayedData, import: true

describe "deserialize/4" do
test "an empty binary results in an error" do
assert {:error, :incomplete} == ArrayedData.deserialize([], 1, Device, <<>>)
end
end
end

0 comments on commit b2ce507

Please sign in to comment.