Repository navigation
Musepack: don't hang on an SV8 packet smaller than its header - #736
Open
Thanatos9404 wants to merge 1 commit into
Open
Thanatos9404 wants to merge 1 commit into
Thanatos9404 wants to merge 1 commit into
Conversation
The SV8 packet size counts the two byte key and the size field itself,
so data_size = frame_size - key_size - slen goes negative when a packet
claims fewer bytes than that. Unknown packets are skipped with
fileobj.seek(data_size, 1), so a negative value seeks backwards. With a
size of 0 it lands back on the same packet key and the loop never ends,
which makes mutagen.File() hang on a 7 byte file like b"MPCK" + b"XX\x00".
Raise MusepackHeaderError("Invalid packet size.") when data_size is
negative. Every remaining packet then moves the file position forward by
at least its header, so the loop always makes progress. A packet with an
empty payload is still accepted.
Fixes quodlibet#433
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #433
Problem
MusepackInfo.__parse_sv8gets the payload size of each SV8 packet withdata_size = frame_size - key_size - slen, because the packet size counts the 2 byte key and the size field itself. A packet that claims fewer bytes than its own header makesdata_sizenegative, and packets other than SH and RG are skipped withfileobj.seek(data_size, 1). With a size of 0 that seek lands exactly on the same packet key again, so the loop never ends:The same happens when the size 0 is written in two bytes (
b"XX\x80\x00"). Sizes 1 and 2 also seek backwards into the header, but the position still moves forward overall, so they don't hang.Fix
When
data_sizeis negative, raiseMusepackHeaderError("Invalid packet size."), the same error already raised when the size field can't be read. ffmpeg's mpc8 demuxer rejects the same case ("Invalid chunk length" inmpc8_read_header). Every accepted packet then moves the file position forward by at least its header, so the loop always makes progress. A packet with an empty payload (data_size == 0) is still accepted, since it moves forward too.SH and RG packets with a negative size already raised an error, but could first call
fileobj.read()with a negative count, which reads the rest of the file. They are now rejected before that.Tests
Added
test_packet_size_smaller_than_headertotests/test_musepack.py, with the size 0 written in one and in two bytes. Onmainit never finishes (I stopped it after 30 seconds); with the change it passes.test_packet_with_empty_payloadchecks that a packet whose size only covers its header still parses.uv run pytestpasses (4057 tests), anduv run flake8anduv run mypy .report nothing.