Skip to content

Commit

Permalink
Raise exception when decoding too large synchsafe integer
Browse files Browse the repository at this point in the history
  • Loading branch information
thebigmunch committed Apr 9, 2020
1 parent 8658360 commit 885b182
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This project adheres to [Semantic Versioning](https://semver.org).
* Emit warning when ignoring ID3v2 frames with bad values.
* Ignore ID3v2 frames with no value.
* Catch and raise more exceptions when parsing ID3v2 frames.
* Raise exception when decoding too large synchsafe integer.

### Fixed

Expand Down
10 changes: 9 additions & 1 deletion src/audio_metadata/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,15 @@ def remove_unsynchronization(data):
def decode_synchsafe_int(data, per_byte):
"""Decode synchsafe integers from ID3v2 tags."""

return reduce(lambda value, element: (value << per_byte) + element, data, 0)
i = reduce(lambda value, element: (value << per_byte) + element, data, 0)

if i > 2 ** (per_byte * 4) - 1:
raise ValueError(
f"{data} is too large to be a synchsafe "
f"integer with {per_byte} bits per byte."
)

return i


def encode_synchsafe_int(i, per_byte):
Expand Down

0 comments on commit 885b182

Please sign in to comment.