Skip to content
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
3 changes: 3 additions & 0 deletions src/compact_encoding/frame.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from . import integer
from .codec import OutOfBounds
from .state import State


Expand All @@ -21,6 +22,8 @@ def decode(self, state):
outer_end = state.end
length = integer.uint.decode(state)
frame_end = state.start + length
if frame_end > outer_end:
raise OutOfBounds("Out of bounds")
state.end = frame_end
try:
m = self._codec.decode(state)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,16 @@ def test_frame_decode_truncated_raises():
# aren't present; verified this also raises against the JS reference.
with pytest.raises(OutOfBounds):
cenc.decode(cenc.frame(cenc.uint), bytes.fromhex("01fd"))


def test_frame_decode_length_past_the_end_raises():
# A frame announcing more bytes than follow it is a rejection rather than
# the value inside it: 0501 announces five bytes with one behind it. The
# cursor must not be left past the buffer either, or a later read works
# from a position that does not exist.
from compact_encoding.state import State

state = State(bytes.fromhex("0501"))
with pytest.raises(OutOfBounds):
cenc.frame(cenc.uint).decode(state)
assert state.remaining >= 0
Loading