Skip to content

Commit 5bb5766

Browse files
haljintony612
authored andcommitted
Raise DecodeError if the binary cannot be parsed (#81)
* Raise DecodeError if the binary cannot be parsed * Change instrumentation
1 parent 70caf73 commit 5bb5766

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/protobuf/decoder.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ defmodule Protobuf.Decoder do
308308
raw_handle_varint(type, rest, result, val)
309309
end
310310

311+
defp raw_decode_varint(_, _, _) do
312+
raise Protobuf.DecodeError, message: "cannot decode binary data"
313+
end
314+
311315
defp raw_handle_varint(:key, <<bin::bits>>, result, key) do
312316
tag = bsr(key, 3)
313317
wire_type = band(key, 7)
@@ -352,6 +356,10 @@ defmodule Protobuf.Decoder do
352356
raw_decode_key(rest, [<<n::64>> | result])
353357
end
354358

359+
def raw_decode_value(_, _, _) do
360+
raise Protobuf.DecodeError, message: "cannot decode binary data"
361+
end
362+
355363
# packed
356364
defp put_packed_field(msg, %{wire_type: wire_type, type: type, name_atom: key}, val) do
357365
vals =

test/protobuf/decoder_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ defmodule Protobuf.DecoderTest do
2828
end)
2929
end
3030

31+
test "raises for bad binaries" do
32+
assert_raise(Protobuf.DecodeError, ~r{cannot decode binary data}, fn ->
33+
Decoder.decode(<<0, 0, 0, 0, 0, 0, 0>>, TestMsg.Foo)
34+
end)
35+
end
36+
3137
test "skips unknown varint fields" do
3238
struct = Decoder.decode(<<8, 42, 32, 100, 45, 0, 0, 247, 66>>, TestMsg.Foo)
3339
assert struct == TestMsg.Foo.new(a: 42, d: 123.5)

0 commit comments

Comments
 (0)