Skip to content

MP4: do not convert an out of range length to int - #1419

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

ufleisch merged 1 commit into
taglib:masterfrom
manuaudio:fix/mp4-mdhd-length-conversion-range

Conversation

@manuaudio

Copy link
Copy Markdown
Contributor

The last format in the class, after #1416, #1417 and #1418.

The mdhd duration is a signed 64 bit field in version 1, and the timescale sitting beside it is a 32 bit field that may be 1. TagLib divides one by the other into a double and converts straight to int with no range check, so a 4 KB file is enough:

site value reported how the file gets there
mp4properties.cpp:207 4.61169e+21 mdhd version 1, timescale 1, duration 2^62
mp4properties.cpp:207 4.29497e+12 mdhd version 0, timescale 1, duration 2^32-1

The mvhd fallback a few lines above feeds the same expression, so the one guard covers it too.

What I checked and left alone

This file has five other conversions that look like the same shape and are not:

  • :241 and :255, the esds and alac nominal bitrates, divide a 32 bit value by 1000.0. The largest possible result is about 4.3e6, comfortably inside int.
  • :244, :262 and :311 estimate a bitrate from calculateMdatLength(), which returns long long. That whole expression is integer arithmetic, so a narrowing there is implementation-defined rather than undefined, and not this bug.

I mention it because "the other five look identical" is the obvious reviewer question, and I would rather answer it up front than have you check.

Regression evidence

  • The 18 MP4 files in tests/data report identical channels, sample rate, bitrate and length before and after.
  • The suite runs 576 tests either way.
  • Measured on this branch alone, rebuilt from master, not on a tree that also carried MPEG: do not convert out of range properties to int #1418 — and the two comparison binaries were hashed to confirm they differ.

The mdhd duration is a signed 64 bit field in version 1 and the
timescale sitting beside it is a 32 bit field that may be 1, so a
4 KB file can declare 2^62 units of a one-hertz clock. The millisecond
length is then ~4.6e21 and the conversion to int is undefined:

  taglib/mp4/mp4properties.cpp:207:34: runtime error: 4.61169e+21 is
  outside the range of representable values of type 'int'

The version 0 path reaches the same line with a 32 bit duration:

  taglib/mp4/mp4properties.cpp:207:34: runtime error: 4.29497e+12 is
  outside the range of representable values of type 'int'

The mvhd fallback a few lines above feeds the same expression, so it is
covered by the same guard.

Leave the field at its default rather than converting.

For the record, the other conversions in this file were checked and are
not affected. The esds and alac nominal bitrates divide a 32 bit value
by 1000.0, which cannot leave int's range, and the three
calculateMdatLength() estimates are integer arithmetic on a long long
rather than a double conversion.

One report per file before the change, none after. The 18 MP4 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 d841424 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