Skip to content

Commit

Permalink
Fix: Avoid infinite loops when parsing chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
mbsantiago committed Aug 21, 2023
1 parent 284cc2b commit aff1dc9
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/soundevent/audio/chunks.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,15 @@ def _get_subchunks(riff: BinaryIO, size: int) -> List[Chunk]:
subchunks = []
while riff.tell() < start_position + size - 1:
subchunk = _read_chunk(riff)

if subchunk is None:
break

subchunks.append(subchunk)
return subchunks


def _read_chunk(riff: BinaryIO) -> Chunk:
def _read_chunk(riff: BinaryIO) -> Chunk | None:
"""Read a chunk from a RIFF file at current pointer position.
We assume the file pointer is at the beginning of the chunk.
Expand All @@ -91,6 +95,10 @@ def _read_chunk(riff: BinaryIO) -> Chunk:
chunk_id = riff.read(4).decode("ascii")
size = int.from_bytes(riff.read(4), "little")

if size == 0:
# Make sure we don't get stuck in an infinite loop.
return None

identifier = None
if chunk_id in CHUNKS_WITH_SUBCHUNKS:
identifier = riff.read(4).decode("ascii")
Expand Down Expand Up @@ -124,6 +132,16 @@ def parse_into_chunks(riff: BinaryIO) -> Chunk:
Returns
-------
Chunk
Raises
------
ValueError
If the RIFF file is empty.
"""
riff.seek(0)
return _read_chunk(riff)
chunk = _read_chunk(riff)

if chunk is None:
raise ValueError("RIFF file is empty")

return chunk

0 comments on commit aff1dc9

Please sign in to comment.