manuaudioandGitHub ee0ab489e0 Validate enumerations decoded from file bytes before casting to them (#1420)
* ID3v2: validate the text encoding byte before casting it

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)

* Validate the picture type byte before casting it

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)

* ID3v2: validate the RVA2 channel byte before casting it

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)

* ID3v2: validate the SYLT and ETCO enum bytes before casting them

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)

* MP4: validate the atom data type before casting it

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)
2026-08-16 12:30:31 +02:00
2026-06-24 05:27:17 +02:00
2026-07-19 19:58:28 +02:00
2022-10-24 07:06:23 +02:00
2026-08-14 06:47:05 +02:00
2015-05-18 11:31:55 +02:00
2023-10-08 05:58:33 +02:00
2023-09-16 08:41:01 +02:00
2026-07-19 19:58:28 +02:00
2026-07-12 20:16:12 +02:00
2024-01-04 17:18:23 +01:00
2026-02-18 05:15:14 +01:00

TagLib

Build Status

TagLib Audio Metadata Library

https://taglib.org/

TagLib is a library for reading and editing the metadata of several popular audio formats. Currently, it supports various metadata containers such as ID3v1, ID3v2 and Vorbis comments for MP3, MP4, AAC, Ogg, Opus, FLAC, Speex, APE, MPC, WavPack, WAV, AIFF, TrueAudio, Matroska, WebM, ASF, WMA, DSF, DFF and tracker (MOD, XM, S3M, IT) files.

TagLib is distributed under the GNU Lesser General Public License (LGPL) and Mozilla Public License (MPL). Essentially that means that it may be used in proprietary applications, but if changes are made to TagLib they must be contributed back to the project. Please review the licenses if you are considering using TagLib in your project.

S
Description
No description provided
Readme
15 MiB
Languages
C++ 96.7%
CMake 2%
C 0.9%
Logos 0.4%