Skip to content

FLAC, APE, Vorbis, Opus, Speex: do not convert out of range properties to int - #1417

Merged
ufleisch merged 5 commits into
taglib:masterfrom
manuaudio:fix/ogg-flac-ape-length-conversion-range
Aug 15, 2026
Merged

ufleisch merged 5 commits into
taglib:masterfrom
manuaudio:fix/ogg-flac-ape-length-conversion-range

Conversation

@manuaudio

Copy link
Copy Markdown
Contributor

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 double from file-controlled values and then converts it to int without 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:

site value reported how the file gets there
flacproperties.cpp:136 6.87195e+13 STREAMINFO: 2^36-1 frames at 1 Hz
flacproperties.cpp:137 1.75922e+10 1 frame at the maximum 20 bit rate, 2 MB stream
apeproperties.cpp:141 4.29497e+12 header frame count ~4.3e9 at 1 Hz
apeproperties.cpp:142 3.60301e+13 1 frame at rate INT_MAX, 2 MB stream
vorbisproperties.cpp:168 4.61169e+21 final granule 2^62, rate 1 in the ident header
vorbisproperties.cpp:169 3.51569e+13 granule 1 at rate INT_MAX, 2 MB stream
opusproperties.cpp:156 9.60768e+16 final granule 2^62
speexproperties.cpp:165 4.61169e+21 final granule 2^62, rate 1 in the Speex header
speexproperties.cpp:166 3.61877e+13 granule 1 at rate INT_MAX, 2 MB stream

Opus 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

  • The 23 FLAC, APE, Ogg Vorbis, Opus and Speex files in tests/data report identical channels, sample rate, bitrate and length before and after.
  • The suite runs 576 tests either way.
  • Both figures were taken from two separately built libraries, verified to differ by hash, so this is not one binary compared against itself.

One note in case it saves you time: the suite cannot run under ASan, because TestByteVector::testFind1 writes one byte past a resized ByteVector on 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 master rather 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.

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)
@ufleisch
ufleisch merged commit c3d4eef into taglib:master Aug 15, 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