Repository navigation
MPEG: do not convert out of range properties to int - #1418
Merged
ufleisch merged 1 commit intoAug 16, 2026
Merged
Conversation
The Xing header supplies both the frame count and the byte count as raw 32 bit values, and TagLib multiplies the frame count by the per-frame duration without checking the result. At MPEG 2.5 Layer III / 8 kHz a frame is 72 ms, so 2^32-1 declared frames puts the length at ~3.1e11 ms and the conversion to int is undefined: taglib/mpeg/mpegproperties.cpp:166:35: runtime error: 3.09238e+11 is outside the range of representable values of type 'int' The bitrate on the next line is reachable the other way round. One declared frame of MPEG 1 Layer I at 48 kHz is 8 ms, and the declared size is not the real file size, so a 1 KB file can claim 2^32-1 bytes: taglib/mpeg/mpegproperties.cpp:167:35: runtime error: 4.29497e+09 is outside the range of representable values of type 'int' The length computed on the non-Xing path has the same shape but is bounded by the real stream length rather than a declared count, so it needs a file of a couple of gigabytes rather than a crafted header. I have not built one; that guard is there for consistency, not on demonstrated evidence. Leave the field at its default rather than converting. Two reports before the change, none after. The 23 MPEG files in tests/data report identical channels, sample rate, bitrate and length before and after, and the suite runs 576 tests either way. Assisted-By: Claude Code (Claude Opus 5)
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.
Third and, for the length/bitrate class, close to last: the MPEG path, following #1416 and #1417.
The Xing header hands TagLib both the frame count and the byte count as raw 32 bit values. Neither is checked against anything, and neither has to match the file it lives in.
Demonstrated
mpegproperties.cpp:1663.09238e+11mpegproperties.cpp:1674.29497e+09The bitrate case is the one I would flag to a reviewer:
totalSizeis the size the Xing header claims, not the size of the file. The proof file is 1.3 KB and declares 4.3 GB. No large input is needed to reach it.Not demonstrated
The length on the non-Xing path (
mpegproperties.cpp:243before this change). Same expression, but bounded by the real stream length instead of a declared count, so reaching it needs a file of a couple of gigabytes rather than a crafted header. I did not build one. It is guarded for consistency and the commit message says so.Regression evidence
tests/datareport identical channels, sample rate, bitrate and length before and after.Building the proof files, in case you want them
Two things make a hand-built MPEG frame harder than it looks, and both are TagLib behaving correctly:
isFrameSync()rejects0xFFas the second header byte, so MPEG 1 Layer I has to carry a CRC (protection bit 0) to be recognised at all.Header::parse(checkLength = true)only accepts a frame when a matching header sits atoffset + frameLength, so every file needs at least two frames, andframeLengthhas to match the bitrate table exactly — my first attempt used 416 kbps for MPEG 1 Layer I index 14 where the table says 448, and the frame was silently skipped.Happy to attach the generator if it is useful for
tests/data.