Skip to content

DSF: do not convert an out of range length to unsigned int - #1416

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

ufleisch merged 5 commits into
taglib:masterfrom
manuaudio:fix/dsf-length-conversion-range

Conversation

@manuaudio

Copy link
Copy Markdown
Contributor

What

DSF::Properties::read computes the length as

d->length = d->samplingFrequency > 0
    ? static_cast<unsigned int>(static_cast<double>(d->sampleCount) * 1000.0 / d->samplingFrequency + 0.5)
    : 0;

sampleCount is a long long read straight from the file and samplingFrequency is an unsigned int from the file, so the millisecond count can land well outside unsigned int. Converting a floating point value the destination type cannot represent is undefined:

taglib/dsf/dsfproperties.cpp:132:35: runtime error: 8.41595e+09 is outside
the range of representable values of type 'unsigned int'
    #0 TagLib::DSF::Properties::read(TagLib::ByteVector const&) dsfproperties.cpp:132
    #1 TagLib::DSF::Properties::Properties(...) dsfproperties.cpp:59
    #10 TagLib::FileRef::FileRef(char const*, bool, ...) fileref.cpp:361

It is on the plain parse path — constructing a FileRef over the file is enough. A negative sampleCount converts just as badly, so the guard covers that too.

The change

Report an unknown length rather than converting, which is what a zero sampling frequency already does.

Nothing changes for a valid file — tests/data/empty10ms.dsf reads lengthMs=10 bitrate=5645 rate=2822400 ch=2 both before and after. Reverting just this hunk and rebuilding brings the report straight back.

I could not run the CppUnit suite (CppUnit isn't installed here, so BUILD_TESTING skips the tests directory) — the checks above are what I have.

bitrate on the line above is fine

Worth saying since it looks similar: samplingFrequency * bitsPerSample * channelNum is unsigned arithmetic, so it wraps rather than overflowing, and dividing by 1000.0 leaves at most ~4.3e6, comfortably inside unsigned int.

One I could not demonstrate

DSDIFF::Properties (dsdiffproperties.cpp:60) has the same shape, casting to int instead. There the sample count comes from a chunk size that chunkFits has already validated against the file length, so triggering it needs a genuinely large file — roughly 262 KB with a sample rate of 1 — rather than a few mutated bytes. I did not build one, so I have left it alone rather than claim it. Happy to follow up if you'd like it guarded on the same grounds.

How it was found

Mutating the files in tests/data and running each through a parse → read every property → modify → save() round trip, under ASan and UBSan.

I calibrated the harness first: with a deliberate one byte over-read injected into FileStream::readBlock, ASan reports it through that path, and the report goes away when the injection is removed. Without that check a clean run would not have meant anything.

Disclosure

Written with Claude Code assisting, under my direction; the commit carries an Assisted-By trailer. I verified the before/after myself on macOS/arm64.

X: manuaudiomix

DSF::Properties::read computes

    d->length = d->samplingFrequency > 0
        ? static_cast<unsigned int>(static_cast<double>(d->sampleCount)
                                    * 1000.0 / d->samplingFrequency + 0.5)
        : 0;

sampleCount is a long long taken straight from the file and
samplingFrequency is an unsigned int from the file, so the millisecond
count can land well outside unsigned int. Converting a floating point
value the destination type cannot represent is undefined:

  taglib/dsf/dsfproperties.cpp:132:35: runtime error: 8.41595e+09 is
  outside the range of representable values of type 'unsigned int'
    #0 TagLib::DSF::Properties::read(TagLib::ByteVector const&)
    taglib#1 TagLib::DSF::Properties::Properties(...)
    taglib#10 TagLib::FileRef::FileRef(char const*, bool, ...)

A negative sampleCount converts just as badly, so guard that too.

Report an unknown length instead of converting, matching what a zero
sampling frequency already does. Nothing changes for a valid file:
tests/data/empty10ms.dsf reads lengthMs=10 bitrate=5645 rate=2822400
ch=2 both before and after.

Found by mutating the files in tests/data and running them through a
parse, read properties and save round trip under UBSan.

Assisted-By: Claude Code (Claude Opus 5)
Same shape as the DSF change: sampleCount comes from a chunk size in the
file and sampleRate is read from it, so

    static_cast<int>(static_cast<double>(d->sampleCount) * 1000.0
                     / d->sampleRate + 0.5)

can be handed a value int cannot represent, which is undefined.

In the PR I said I had not demonstrated this one, because chunkFits
bounds the sample count by the file length so a few mutated bytes will
not do it. Constructing the file it does need - a 700 kB DSD chunk with
a sample rate of 1 - reaches it:

  taglib/dsdiff/dsdiffproperties.cpp:61:24: runtime error: 2.8e+09 is
  outside the range of representable values of type 'int'

Two reports before the change, none after. tests/data/empty10ms.dff is
unchanged at lengthMs=10 bitrate=5644 rate=2822400 ch=2.

Assisted-By: Claude Code (Claude Opus 5)
Third instance of the same shape as the DSF and DSDIFF changes:

    static_cast<int>(static_cast<double>(duration) / 10000.0
                     - static_cast<double>(preroll) + 0.5)

duration and preroll are both long long values read from the file
properties object, so the result can land outside int, and converting a
double the destination type cannot represent is undefined:

  taglib/asf/asffile.cpp:240:22: runtime error: -2.80375e+14 is outside
  the range of representable values of type 'int'

Only skip the conversion when the value will not fit. Values that do
fit, negative ones included, are set exactly as before, so the three
.wma files in tests/data still report lengthMs 3549, 96502 and 3712.

Found by the same fuzzing that turned up the DSF case.

Assisted-By: Claude Code (Claude Opus 5)
@manuaudio

Copy link
Copy Markdown
Contributor Author

Continued fuzzing turned up a third instance of the same shape, so I have added it here rather than opening another PR — ASF::File computes its length as

static_cast<int>(static_cast<double>(duration) / 10000.0 - static_cast<double>(preroll) + 0.5)

with duration and preroll both read from the file properties object:

taglib/asf/asffile.cpp:240:22: runtime error: -2.80375e+14 is outside
the range of representable values of type 'int'

The guard only skips the conversion when the value will not fit, so values that do fit — negative ones included — are set exactly as before. The three .wma files in tests/data still report lengthMs 3549, 96502 and 3712, unchanged.

That makes three commits, each a self-contained instance of "a length computed from file-supplied numbers is converted to a type that cannot represent it": DSF, DSDIFF, ASF. Happy to split them if you would rather take them separately.


Separately, and not in this PR: the same fuzzing found that the ID3v2 frame parsers cast the text encoding byte straight out of the file,

d->textEncoding = static_cast<String::Type>(data[0]);

with no check that the byte is one of the five values String::Type defines. UBSan reports it at parse time, and the invalid value then flows into Frame::checkTextEncoding, renderFields and String::data — the last of which switches on the type with no default: arm:

textidentificationframe.cpp:226: runtime error: load of value 4294967269,
which is not a valid value for type 'Type'

There are eleven of these across nine files, plus a twelfth of the same kind for AttachedPictureFrame::Type. I have a change ready that validates the byte in one place and falls back to String::Latin1 — which is what the frames already declare as their default — and leaves the tags of all 30 tagged files in tests/data byte-identical.

I have deliberately not opened it, because where the helper belongs (tutils.h seemed natural) and whether an unknown byte should fall back or reject the frame feel like your calls rather than mine. Say the word and I will send it, or open an issue with the reproducer if you would rather keep it as a report.

@manuaudio

Copy link
Copy Markdown
Contributor Author

Two more of the same shape turned up, so this is now five commits. Both verified the same way — reverting only the hunk brings the report back, and the affected files in tests/data report identical properties either way.

AIFF (aiffproperties.cpp) — the sample rate is an 80 bit float read from the file, so it reaches values with no realistic bound:

aiffproperties.cpp:145:38: runtime error: 6.29416e+49 is outside the range of ... 'int'
aiffproperties.cpp:150:35: runtime error: 1.00707e+48 is outside the range of ... 'int'

All seven AIFF files in tests/data unchanged.

MPC (mpcproperties.cpp, readSV8) — frameCount is sampleFrames - begSilence, both from the file:

mpcproperties.cpp:244:39: runtime error: 4.18294e+17 is outside the range of ... 'int'

This one showed up independently from two different seed files, sv8_header.mpc and zerodiv.mpc.

So the five are DSF, DSDIFF, ASF, AIFF and MPC — one commit each, all "a length or rate computed from file-supplied numbers is converted to a type that cannot represent it". Happy to split, squash or drop any of them.


On the enum question I raised above, I have finished the survey and it is wider than the eleven ID3v2 sites I mentioned. Three different enums are built by casting a file-supplied value without checking it:

enum sites valid range
String::Type 11, across 9 files Latin1..UTF16LE, 0–4
AttachedPictureFrame::Type 2 — attachedpictureframe.cpp:147, 208 0–20
MP4::AtomDataType 2 — mp4itemfactory.cpp:455, 464 up to TypeUndefined = 255

Observed values were 4294967269 for the first, and 3321888768 for AtomDataType. Loading an enumeration object whose value is outside the enumeration is undefined, and in the String::Type case the value then reaches String::data(), which switches on it with no default: arm.

Still not opening anything for this — it is fifteen sites and the fallback policy is a design decision. Just wanted the survey to be complete rather than leave you with a partial number.

Fourth instance of the same shape. The sample rate is an 80 bit float
read from the file and the frame count is a 32 bit count from it, so all
three conversions here can be handed a value int cannot represent:

  taglib/riff/aiff/aiffproperties.cpp:145:38: runtime error: 6.29416e+49
  is outside the range of representable values of type 'int'
  taglib/riff/aiff/aiffproperties.cpp:150:35: runtime error: 1.00707e+48
  is outside the range of representable values of type 'int'

Line 149 is the same shape and reachable the other way round, with a
large sampleFrames over a small sample rate, so it is guarded too.

Leave the field at its default rather than converting. All seven AIFF
files in tests/data report identical channels, sample rate, bitrate and
length before and after.

Two reports before the change, none after.

Assisted-By: Claude Code (Claude Opus 5)
Fifth instance of the same shape, in readSV8:

    const auto length = static_cast<double>(frameCount) * 1000.0 / d->sampleRate;
    d->length  = static_cast<int>(length + 0.5);
    d->bitrate = static_cast<int>(static_cast<double>(streamLength) * 8.0 / length + 0.5);

frameCount is sampleFrames minus begSilence, both read from the file, so
the millisecond figure can land outside int:

  taglib/mpc/mpcproperties.cpp:244:39: runtime error: 4.18294e+17 is
  outside the range of representable values of type 'int'

The bitrate on the next line goes the same way when length is small, so
it is guarded too. One report before, none after, and the .mpc files in
tests/data report identical properties.

Turned up independently from two different seed files, sv8_header.mpc
and zerodiv.mpc.

Assisted-By: Claude Code (Claude Opus 5)
@ufleisch
ufleisch force-pushed the fix/dsf-length-conversion-range branch from 5049fec to d1c0327 Compare August 15, 2026 15:50
@ufleisch

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

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