Repository navigation
Conversation
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.
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 |
Contributor
Author
|
That is better, thanks — taking the raw |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 —TimestampFormatspans only 0..3, the picture type 0..31.String::TypeRelativeVolumeFrame::ChannelTypeSynchronizedLyricsFrame::TimestampFormat+::Type,EventTimingCodesFrame::TimestampFormatMP4::AtomDataTypeEach 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 -1FLAC::Picture::type()returns -1, or 65536 for a 32-bit valueFLAC::Pictureis the widest of the three, since it casts a whole 32-bit field rather than a byte. AndString::data()switches on the encoding with nodefault:arm, so an unrecognised encoding silently returns an emptyByteVectoron 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 atypeFromByte()alongside the existingtypeFromString(), backed byUtils::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:109reads one line below a site I did change and looks identical:EventTypeenumerates up to0xFE, so its range is 0..255 and anunsigned charcannot leave it. That inner cast is exactly what the line above was missing. Not a bug, not touched.Also unchanged:
typeFromString()(pictureTypeFromStringonly ever returns 0..20) andtvariant.cpp:289(casts astd::variantindex).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::Typeenumerates 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
channel=1, SYLTtsFormat=2 type=1, picturetype=3.tests/data— audio properties, picture types, RVA2 channels, SYLT/ETCO fields and MP4 items — all byte-identical tomaster. The comparison is not vacuous: it includes realAPIC type=3and realcovritems.Happy to split this into separate PRs per enumeration if that is easier to review.