Skip to content

Commit

Permalink
has_subblock() should check for end of block
Browse files Browse the repository at this point in the history
  • Loading branch information
ricklupton committed Sep 16, 2023
1 parent 74e359a commit 9bf093d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ New features:
Other changes:
- The `value` attribute of scene item blocks, which was not being used, has been
removed.
- Check more carefully for sub-blocks
([#17](https://github.com/ricklupton/rmscene/issues/17#issuecomment-1701071477)).

### v0.4.0

Expand Down
6 changes: 6 additions & 0 deletions src/rmscene/tagged_block_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ def read_subblock(self, index: int) -> Iterator[SubBlockInfo]:

def has_subblock(self, index: int) -> bool:
"""Check if a subblock with the given index is next."""
# It's possible that if we are at the end of the block, the next bytes
# (the size of the next block) could happen to match the tag and index
# of what we are looking for -- so check explicitly for end of block.
if self.current_block:
if self.bytes_remaining_in_block() <= 0:
return False
return self.data.check_tag(index, TagType.Length4)

def _check_position(self, block_info: BlockInfo):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_tagged_block_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,21 @@ def test_read_string_utf():
s = stream("1c05000000" "030161c397")
result = s.read_string(1)
assert result == "a×"


def test_has_subblock_checks_for_end_of_block():
# See https://github.com/ricklupton/rmscene/issues/17#issuecomment-1701071477
#
# Construct some potentially confusing data -- the 0x2c is the start of the
# next block, but if we don't take care, `has_subblock(2)` could see it as a
# subblock instead.
data_hex = """
03000000 00010103
1f 0219
2c000000 00010100
"""

s = stream(data_hex)
with s.read_block():
assert s.read_id(1)
assert s.has_subblock(2) == False

0 comments on commit 9bf093d

Please sign in to comment.