Skip to content

MPEG: do not convert out of range properties to int - #1418

Merged
ufleisch merged 1 commit into
taglib:masterfrom
manuaudio:fix/mpeg-xing-length-conversion-range
Aug 16, 2026
Merged

ufleisch merged 1 commit into
taglib:masterfrom
manuaudio:fix/mpeg-xing-length-conversion-range

Conversation

@manuaudio

Copy link
Copy Markdown
Contributor

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

site value reported how the file gets there
mpegproperties.cpp:166 3.09238e+11 MPEG 2.5 Layer III at 8 kHz is 72 ms per frame; Xing declares 2^32-1 frames
mpegproperties.cpp:167 4.29497e+09 MPEG 1 Layer I at 48 kHz is 8 ms per frame; Xing declares 1 frame and 2^32-1 bytes

The bitrate case is the one I would flag to a reviewer: totalSize is 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:243 before 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

  • The 23 MPEG files in tests/data report identical channels, sample rate, bitrate and length before and after.
  • The suite runs 576 tests either way.
  • Both figures came from two separately built libraries whose hashes I checked differ.

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() rejects 0xFF as 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 at offset + frameLength, so every file needs at least two frames, and frameLength has 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.

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)
@ufleisch
ufleisch merged commit be42495 into taglib:master Aug 16, 2026
4 checks passed
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.

2 participants