Repository navigation
FLAC, APE, Vorbis, Opus, Speex: do not convert out of range properties to int - #1417
Merged
ufleisch merged 5 commits intoAug 15, 2026
Merged
Conversation
STREAMINFO carries the total sample count in a 36 bit field and the sample rate in a 20 bit one, so a structurally valid file can declare 2^36-1 frames at 1 Hz. The millisecond length is then ~6.9e13 and the conversion to int is undefined: taglib/flac/flacproperties.cpp:136:35: runtime error: 6.87195e+13 is outside the range of representable values of type 'int' The bitrate on the next line is reachable the other way round, with a short stream at a high declared rate making the divisor tiny: taglib/flac/flacproperties.cpp:137:35: runtime error: 1.75922e+10 is outside the range of representable values of type 'int' Leave the field at its default rather than converting. Two reports before the change, none after. The 23 FLAC, APE, Ogg Vorbis, Opus and Speex 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)
The frame count is assembled from three 32 bit header fields and the sample rate is read straight from the header, so a file can declare ~4.3e9 frames at 1 Hz. The millisecond length is then ~4.3e12 and the conversion to int is undefined: taglib/ape/apeproperties.cpp:141:35: runtime error: 4.29497e+12 is outside the range of representable values of type 'int' The bitrate on the next line is reachable the other way round, with one frame at a high declared rate making the divisor tiny: taglib/ape/apeproperties.cpp:142:35: runtime error: 3.60301e+13 is outside the range of representable values of type 'int' Leave the field at its default rather than converting. Two reports before the change, none after. The 23 FLAC, APE, Ogg Vorbis, Opus and Speex 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)
The length comes from the difference between two 64 bit granule positions over a sample rate read from the identification header, so a file can declare 2^62 samples at 1 Hz. The millisecond length is then ~4.6e21 and the conversion to int is undefined: taglib/ogg/vorbis/vorbisproperties.cpp:168:39: runtime error: 4.61169e+21 is outside the range of representable values of type 'int' The bitrate on the next line is reachable the other way round, with one sample at a high declared rate making the divisor tiny: taglib/ogg/vorbis/vorbisproperties.cpp:169:39: runtime error: 3.51569e+13 is outside the range of representable values of type 'int' Leave the field at its default rather than converting. Two reports before the change, none after. The 23 FLAC, APE, Ogg Vorbis, Opus and Speex 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)
Opus is the one worth reading twice: its granule clock is fixed at 48 kHz, so unlike the other formats no absurd declared sample rate is needed. A 64 bit granule position alone is enough. At 2^62 samples the millisecond length is ~9.6e16 and the conversion to int is undefined: taglib/ogg/opus/opusproperties.cpp:156:39: runtime error: 9.60768e+16 is outside the range of representable values of type 'int' The bitrate on the next line has the same shape as the other formats, but because the divisor cannot be made arbitrarily small at a fixed 48 kHz it needs a stream of several megabytes to overflow, so I have not built a test file for it. It is guarded for consistency rather than on demonstrated evidence. Leave the field at its default rather than converting. One report before the change, none after. The 23 FLAC, APE, Ogg Vorbis, Opus and Speex 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)
Same shape as Vorbis. The length comes from the difference between two 64 bit granule positions over a sample rate read from the Speex header, so a file can declare 2^62 samples at 1 Hz. The millisecond length is then ~4.6e21 and the conversion to int is undefined: taglib/ogg/speex/speexproperties.cpp:165:39: runtime error: 4.61169e+21 is outside the range of representable values of type 'int' The bitrate on the next line is reachable the other way round, with one sample at a high declared rate making the divisor tiny: taglib/ogg/speex/speexproperties.cpp:166:39: runtime error: 3.61877e+13 is outside the range of representable values of type 'int' Leave the field at its default rather than converting. Two reports before the change, none after. The 23 FLAC, APE, Ogg Vorbis, Opus and Speex 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 was referenced Aug 15, 2026
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.
Five more instances of the conversion class in #1416, in the formats that PR did not touch: FLAC, APE, Ogg Vorbis, Opus and Speex.
Each of these computes a millisecond length as a
doublefrom file-controlled values and then converts it tointwithout checking the range, and converting a double the destination type cannot represent is undefined. The bitrate on the following line is the same defect reached from the other side: a short stream at a high declared rate makes the divisor tiny, so the quotient overflows instead.Demonstrated
Nine UBSan reports, each from a purpose-built file, all gone after the change:
flacproperties.cpp:1366.87195e+13flacproperties.cpp:1371.75922e+10apeproperties.cpp:1414.29497e+12apeproperties.cpp:1423.60301e+13INT_MAX, 2 MB streamvorbisproperties.cpp:1684.61169e+21vorbisproperties.cpp:1693.51569e+13INT_MAX, 2 MB streamopusproperties.cpp:1569.60768e+16speexproperties.cpp:1654.61169e+21speexproperties.cpp:1663.61877e+13INT_MAX, 2 MB streamOpus is the one worth reading twice. Its granule clock is fixed at 48 kHz, so it needs no absurd declared sample rate — a 64 bit granule position on its own is enough. That makes it the most plausible of the five to meet in the wild.
Not demonstrated
opusproperties.cpp:157, the Opus bitrate. Because the divisor cannot be made arbitrarily small at a fixed 48 kHz clock, overflowing it needs a stream of several megabytes, and I did not think a test file that size earned its place. It is guarded for consistency with its four siblings, not on evidence. Say the word if you would rather I drop that half of the hunk.Regression evidence
tests/datareport identical channels, sample rate, bitrate and length before and after.One note in case it saves you time: the suite cannot run under ASan, because
TestByteVector::testFind1writes one byte past a resizedByteVectoron purpose. That is deliberate and I have left it alone — I mention it only so the abort is not mistaken for a regression. I ran the suite in a plain build and used the sanitizer build only for the reports above.Housekeeping
Based on
masterrather than on #1416, since the two touch disjoint files and I did not want to stack them. Happy to fold these commits into #1416 instead if you would rather review one branch — just say so and I will move them.