Skip to content

Validate enumerations decoded from file bytes before casting to them - #1420

Merged
ufleisch merged 6 commits into
taglib:masterfrom
manuaudio:fix/validate-enums-decoded-from-file-bytes
Aug 16, 2026
Merged

ufleisch merged 6 commits into
taglib:masterfrom
manuaudio:fix/validate-enums-decoded-from-file-bytes

Conversation

@manuaudio

Copy link
Copy Markdown
Contributor

This is the class I mentioned on #1416. That PR merged without an answer either way, so rather than ask again I have built it out with the evidence attached.

Seven enumerations are decoded from file bytes by a plain static_cast. Loading an enumeration object whose value lies outside the range of the enumeration is undefined, and these ranges are small — TimestampFormat spans only 0..3, the picture type 0..31.

commit enumeration sites
1 String::Type 11
2 the shared picture type 4, across ID3v2 / FLAC / ASF
3 RelativeVolumeFrame::ChannelType 1
4 SynchronizedLyricsFrame::TimestampFormat + ::Type, EventTimingCodesFrame::TimestampFormat 3
5 MP4::AtomDataType 2

Each commit quotes the UBSan report it removes and how the file reaches it.

Not just a sanitizer complaint

Two of these are visible through the public API. On a file whose picture type byte is 0xFF:

  • AttachedPictureFrame::type() returns -1
  • FLAC::Picture::type() returns -1, or 65536 for a 32-bit value

FLAC::Picture is the widest of the three, since it casts a whole 32-bit field rather than a byte. And String::data() switches on the encoding with no default: arm, so an unrecognised encoding silently returns an empty ByteVector on the render path.

One helper for the picture type, not three

The picture type enumeration comes from DECLARE_PICTURE_TYPE_ENUM, so the same defect is copied into three classes. Rather than three separate checks, the macro gains a typeFromByte() alongside the existing typeFromString(), backed by Utils::pictureTypeFromByte(). If you would prefer three local checks and no change to the shared header, that is an easy rework — say the word.

What I checked and deliberately left alone

eventtimingcodesframe.cpp:109 reads one line below a site I did change and looks identical:

auto type = static_cast<EventType>(static_cast<unsigned char>(data[pos++]));

EventType enumerates up to 0xFE, so its range is 0..255 and an unsigned char cannot leave it. That inner cast is exactly what the line above was missing. Not a bug, not touched.

Also unchanged: typeFromString() (pictureTypeFromString only ever returns 0..20) and tvariant.cpp:289 (casts a std::variant index).

One precision worth stating, since it affects what the guards must do: a value that is unnamed but in range is not undefined. Text encoding byte 5 produces no sanitizer report at all, because String::Type enumerates 0..4 and its range is therefore 0..7. It is still not a valid encoding, and the helper rejects it — but on correctness grounds, not on undefined behaviour.

Verification

  • 19 purpose-built files covering every enumeration: 16 UBSan reports before, 0 after.
  • Controls keep their valid values — RVA2 channel=1, SYLT tsFormat=2 type=1, picture type=3.
  • Regression checked four ways over the 123 files in tests/data — audio properties, picture types, RVA2 channels, SYLT/ETCO fields and MP4 items — all byte-identical to master. The comparison is not vacuous: it includes real APIC type=3 and real covr items.
  • Suite runs 576 tests either way.
  • Every before/after binary was hash-checked as actually different before its output was compared.

Happy to split this into separate PRs per enumeration if that is easier to review.

manuaudio and others added 6 commits August 15, 2026 12:07
The first byte of most ID3v2 frames selects the text encoding and is
cast straight to String::Type. It comes from the file, so it can be any
value, and String::Type enumerates 0..4 — loading an enumeration object
whose value is outside the enumeration's range is undefined:

  runtime error: load of value 127, which is not a valid value for type
  'String::Type'
  runtime error: load of value 4294967295, which is not a valid value
  for type 'String::Type'

The second value is 0xFF read through a plain signed char.

It matters beyond the sanitizer: String::data() switches on the type
with no default case, so an unrecognised encoding silently falls
through and returns an empty ByteVector on the render path.

Add Utils::textEncodingFromByte(), which maps the byte to its
String::Type or falls back to Latin1 — the encoding these frames
already declare as their default — and use it at the eleven sites that
read the byte from a file.

Note that a byte of 5, 6 or 7 is *not* undefined, because the range of
an enumeration is the bit width spanned by its enumerators rather than
the enumerators themselves. It is still not a valid encoding, and the
same helper rejects it.

Assisted-By: Claude Code (Claude Opus 5)
The picture type enumeration is declared by DECLARE_PICTURE_TYPE_ENUM
and shared by three classes, and all three cast a file-supplied value
to it without checking. The enumerators run 0x00..0x14, so the range of
the enumeration is 0..31 and a load outside that is undefined:

  taglib/mpeg/id3v2/frames/attachedpictureframe.cpp:96:13: runtime
  error: load of value 4294967295, which is not a valid value for type
  'AttachedPictureFrame::Type'
  taglib/flac/flacpicture.cpp:129:13: runtime error: load of value
  65536, which is not a valid value for type 'Type'

FLAC is the widest of the three: it casts a whole 32 bit field, so no
truncation to a byte limits it. ASF and ID3v2 read a plain signed char,
which reaches the same place through sign extension.

This is visible to callers, not only to a sanitizer. Before the change
AttachedPictureFrame::type() and FLAC::Picture::type() return -1 and
65536 for the files above; after it they return Other.

Since the enumeration comes from a macro, add one typeFromByte() to the
macro backed by Utils::pictureTypeFromByte(), rather than three copies
of the same check.

Six reports before the change, none after.

Assisted-By: Claude Code (Claude Opus 5)
Each channel record in a relative volume frame starts with a channel
type byte that is cast straight to ChannelType. The enumerators run
0x00..0x08, so the range of the enumeration is 0..15 and the byte from
the file can leave it:

  runtime error: load of value 127, which is not a valid value for type
  'RelativeVolumeFrame::ChannelType'
  runtime error: load of value 4294967295, which is not a valid value
  for type 'RelativeVolumeFrame::ChannelType'

The value is also used as a map key, so the frame ends up holding a
channel that channels() then reports back.

Map an unrecognised byte to Other instead.

Two reports before the change, none after. The 123 files in tests/data
produce identical channel output either way.

Assisted-By: Claude Code (Claude Opus 5)
Synchronised lyrics carry a timestamp format byte and a content type
byte, and event timing codes carry a timestamp format byte. All three
are cast without checking. TimestampFormat enumerates 0..2, so its
range is only 0..3:

  runtime error: load of value 127, which is not a valid value for type
  'SynchronizedLyricsFrame::TimestampFormat'
  runtime error: load of value 127, which is not a valid value for type
  'SynchronizedLyricsFrame::Type'
  runtime error: load of value 127, which is not a valid value for type
  'EventTimingCodesFrame::TimestampFormat'

with 4294967295 in place of 127 when the byte is 0xFF. Before the
change timestampFormat() and type() return -1 for such a file.

Map an unrecognised byte to Unknown and Other respectively.

Worth noting what is *not* changed: the event type byte on the line
below the ETCO cast is already written
static_cast<EventType>(static_cast<unsigned char>(...)), and EventType
enumerates up to 0xFE, so an unsigned char cannot leave its range. It
reads like the same defect and is not one.

Three reports before the change, none after. The 123 files in
tests/data produce identical output either way.

Assisted-By: Claude Code (Claude Opus 5)
The type field of an iTunes metadata atom is a 32 bit value read from
the file and cast straight to AtomDataType, whose enumerators run
0..255. Values above 255 are outside the range of the enumeration:

  taglib/mp4/mp4atom.h:85:36: runtime error: load of value 65536, which
  is not a valid value for type 'AtomDataType'
  taglib/mp4/mp4atom.h:85:36: runtime error: load of value 4294967295,
  which is not a valid value for type 'AtomDataType'

Both cast sites are reachable: parseFreeForm calls parseData2 with
expectedFlags = -1, so the flags == expectedFlags test never
constrains the value, and the mean/name branch casts unconditionally. A
'----' atom with an arbitrary flags field reaches both.

Map anything outside the range to TypeUndefined, which the enumeration
already provides for exactly this.

Four reports before the change, none after. The 123 files in tests/data
produce identical item output either way.

Assisted-By: Claude Code (Claude Opus 5)
Functions ending with FromByte() shall accept a char argument - the
type returned by ByteVector::operator[]. Use FromUInt() for functions
accepting an unsigned int.

Reorder tutils.h includes to follow existing style.
@ufleisch

Copy link
Copy Markdown
Contributor

Sorry, you are right, I missed the comment about enums in #1416. As AI generated comments tend to be very informative, I usually stop reading when I have understood the code changes.

I have cleaned up the code a bit so that FromByte methods do not surprise with taking an unsigned int. Just tell me if this is not OK.

@manuaudio

Copy link
Copy Markdown
Contributor Author

That is better, thanks — taking the raw char and doing the static_cast<unsigned char> inside typeFromByte means no call site can sign-extend, and the FLAC 4-byte field is correctly on typeFromUInt. Fine by me. Noted on comment length, I will keep them shorter.

@ufleisch
ufleisch merged commit ee0ab48 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