Skip to content

Musepack: don't hang on an SV8 packet smaller than its header - #736

Open
Thanatos9404 wants to merge 1 commit into
quodlibet:mainfrom
Thanatos9404:fix/musepack-sv8-packet-size-loop
Open

Thanatos9404 wants to merge 1 commit into
quodlibet:mainfrom
Thanatos9404:fix/musepack-sv8-packet-size-loop

Conversation

@Thanatos9404

Copy link
Copy Markdown

Fixes #433

Problem

MusepackInfo.__parse_sv8 gets the payload size of each SV8 packet with data_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 makes data_size negative, and packets other than SH and RG are skipped with fileobj.seek(data_size, 1). With a size of 0 that seek lands exactly on the same packet key again, so the loop never ends:

import io, mutagen
mutagen.File(io.BytesIO(b"MPCK" + b"XX\x00"))  # never returns

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_size is negative, raise MusepackHeaderError("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" in mpc8_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_header to tests/test_musepack.py, with the size 0 written in one and in two bytes. On main it never finishes (I stopped it after 30 seconds); with the change it passes. test_packet_with_empty_payload checks that a packet whose size only covers its header still parses. uv run pytest passes (4057 tests), and uv run flake8 and uv run mypy . report nothing.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Timeout while processing an mupack file

1 participant