Skip to content

fix: Drop a terminating NUL from ID3 text - #136

Merged
decriptor merged 29 commits into
mainfrom
fix/id3-trailing-nul
Nov 22, 2026
Merged

decriptor merged 29 commits into
mainfrom
fix/id3-trailing-nul

Conversation

@decriptor

@decriptor decriptor commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

A trailing ID3 terminator is not part of the string. sample.mp3 and sample.wav comment is yepa. The single-value getter returns the first value, so null_title_v2.mp3 title is T. A leading NUL does not hide the next value: the ID3v2 and Vorbis single-value getters (and the public GetTextFrame, GetUserText, VorbisComment.GetValue) return the first non-empty value (ID3v2.4 §4.2 multiple strings), and "" only when every value is empty. A value is kept as written (only an empty one is skipped), and GetTextFrameValues and the ID3v2 list properties follow the same rule for NUL-separated values, so for those the single value and the first list entry agree. A value with / and no NUL is still split on / and trimmed in any version (AC/DC reads Artist AC/DC, Performers [AC, DC]), as before. This deliberately differs from mutagen and TagLib#, which return the empty first ID3v2 value. The comment and lyrics read the first non-empty frame without a description, then the first other one, as ffprobe and TagLib# show it. Each UTF-16 value may carry its own BOM. A comment or unsynchronized lyric ends at the first NUL. A synchronized-lyric NUL ends that line only.

APE list fields keep every performer, album artist, composer, and genre, NUL-separated. The singular getters return the first of those names, not a comma-joined string. A Basic copy from APE keeps that first name. Setting or clearing album artists removes both item names, so a stale value does not remain. Clearing the year also removes Date; setting a new year also writes it to an existing Date. A text album artist on either item wins over a locator. A lone locator or an empty text item reads the same string for a name as it does for a title. A binary item reads as null.

A copy into MP4, ASF, RIFF INFO, or ID3v1 still stores one name: the first non-empty one, so ARTIST= + ARTIST=V2 stores V2 under the default All copy too. That is the single-value contract on those tags. A copy into ID3v2 keeps the full list. Real list support for MP4 and ASF is #137.

Clearing a Vorbis original release date also removes ORIGINALYEAR, and a new date rewrites an existing ORIGINALYEAR to its four-digit year. Writing back the APE year, Vorbis original release date, or Vorbis movement already read changes nothing, so a Picard or Mp3tag tag keeps MOVEMENTNAME, ORIGINALYEAR, and Date.

Setting a new ID3 original release date, or clearing it, removes both TDOR and TORY. A movement write stores the same name in MVNM and TXXX:MOVEMENT. Setting one half of MVIN keeps the other half.

Writing back the value a single-value property returned changes nothing (ordinal comparison), in every ID3v2 string property (text and TXXX frames, year, comment, lyrics, podcast URL, MusicBrainz recording ID), every APE text property, and every Vorbis text field. So an editor that writes back what it showed keeps every value of a multi-value artist, album artist, composer, genre, label or other field, and keeps the fallback fields. = "" and null clear (for a comment or lyrics, every frame without a description, so a described frame such as ID3v1 Comment is still read afterwards, as before; for Vorbis "" leaves one empty field), and any other value replaces the list. An empty MOVEMENT, ORIGINALDATE, APE Year, or ID3 MVNM no longer hides a filled fallback, and a TDOR or TORY without a date no longer hides the other frame.

CopyTo reads every source value first, then writes. It replaces the target's list with the source value, even when that equals the target's first value. A list (performers, album artists, composers, genres, sort and role lists) the target already holds is left as it is; with Extended the artist and genre travel in those lists, so only a Basic copy writes a single name over a list. When the source and target read from a common tag (t.CopyTo (t), mp3.Tag.CopyTo (mp3.Id3v2Tag), mp3.Tag.CopyTo (mp3.Tag) though each read is a new view, flac.Tag.CopyTo (flac.VorbisComment), aiff.Tag.CopyTo (aiff.Id3v2Tag)), every value the target already shows is skipped, so only values it lacks arrive. An empty source value still clears the field, as = "" does, so through a view it also clears what the view showed from another tag (an ID3v1 title, an AIFF NAME chunk). In the other direction, mp3.Id3v2Tag.CopyTo (mp3.Tag) no longer rewrites ID3v1 with values the combined view already shows from ID3v2. A fallback field of its own beside a value equal to the copied one stays (Vorbis ORIGINALYEAR, APE Date, ID3v2.3 TDOR beside TORY). Pictures are outside these rules, as before: a non-empty source picture list replaces the target's.

Vorbis and APE movements follow MusicBrainz Picard's tag mapping: the name is MOVEMENTNAME, the number MOVEMENT (MOVEMENTNUMBER is still read first and kept in step). New names and numbers are written to those keys. A MOVEMENT that is not a number still reads as the name when there is no MOVEMENTNAME. Clearing one half never changes how the other half reads, and a copy from a Picard FLAC no longer writes "2" over MVNM.

Fixes #128

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.64979% with 86 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.34%. Comparing base (304bc06) to head (2c5b459).
⚠️ Report is 19 commits behind head on main.

Files with missing lines Patch % Lines
...agLibSharp2.Tests/Core/TagCopyReplacesListTests.cs 85.76% 38 Missing and 5 partials ⚠️
...agLibSharp2.Tests/Id3/Id3v2/Id3v2WriteBackTests.cs 96.44% 5 Missing and 4 partials ⚠️
src/TagLibSharp2/Core/Tag.cs 93.49% 4 Missing and 4 partials ⚠️
...sts/TagLibSharp2.Tests/Ape/ApeTagWriteBackTests.cs 95.57% 3 Missing and 2 partials ⚠️
...agLibSharp2.Tests/Core/TagCopyListToSingleTests.cs 92.98% 2 Missing and 2 partials ⚠️
...ibSharp2.Tests/Xiph/VorbisCommentWriteBackTests.cs 94.28% 2 Missing and 2 partials ⚠️
src/TagLibSharp2/Ape/ApeTag.cs 98.21% 1 Missing and 2 partials ⚠️
tests/TagLibSharp2.Tests/Ape/ApeTagTests.cs 98.84% 2 Missing and 1 partial ⚠️
...LibSharp2.Tests/Core/PicardMovementMappingTests.cs 95.52% 1 Missing and 2 partials ⚠️
src/TagLibSharp2/Id3/Id3v2/Frames/UserUrlFrame.cs 60.00% 2 Missing ⚠️
... and 2 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #136      +/-   ##
==========================================
+ Coverage   93.07%   93.34%   +0.27%     
==========================================
  Files         298      319      +21     
  Lines       61380    66207    +4827     
  Branches     6067     6752     +685     
==========================================
+ Hits        57129    61803    +4674     
- Misses       2420     2462      +42     
- Partials     1831     1942     +111     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@decriptor
decriptor force-pushed the fix/id3-trailing-nul branch from a194d18 to 4cc0afb Compare November 21, 2026 07:12
@decriptor
decriptor force-pushed the fix/id3-trailing-nul branch 2 times, most recently from abe696c to 203bbbf Compare November 22, 2026 01:55
The single-value composer and album artist are the first entry. Copying
them after the array replaced the list with that one name.

Fixes #128
A Basic copy still copies only the first artist and genre. That is the
Basic contract. APE overrides of the performer, album artist, composer,
and genre lists are what keep the extra names.

UTF-16 values each use their own BOM. A comment or lyric ends at the
first NUL. A leading NUL is an empty first value, not a skipped one.
Setting or clearing album artists removes both Album Artist and
ALBUMARTIST. Artist, album artist, composer, and genre return the first
name. A copy into MP4 or ASF still keeps only that first name.
Setting or clearing an APE year also removes Date, so an old date
cannot come back. A locator or an empty text item reads the same
string for a name as it does for a title. A Vorbis original-release
write also removes ORIGINALYEAR, and a movement write also removes
MOVEMENTNAME.

The changelog records that an APE artist is the first name, and that
a Basic copy into MP4, ASF, RIFF INFO, or ID3v1 keeps that first name.
AlbumArtist returned a locator URL as soon as Album Artist had one,
while AlbumArtists skipped that non-text item and read ALBUMARTIST.
A text name on either key is the first album artist. A locator is
used only when neither key has text.
Original release date reads TDOR or TORY, but a write only replaced one
of them, so clearing the property brought the other frame back. Movement
reads MVNM or MVIN before the TXXX field, so a write to TXXX left the
Apple frame in front. A write now updates both, and a clear removes both.
Setting one half of MVIN keeps the other half.
Clearing the number from MVIN "1/4" alone must keep the total 4, which
only the TXXX:MOVEMENTTOTAL rewrite preserves once MVIN is removed. The
existing step now uses a total that differs from the number, so a swap
of the two halves fails.
A Vorbis movement or original release date write removed MOVEMENTNAME
or ORIGINALYEAR even when the value was set, not cleared. Picard keeps
the movement name in MOVEMENTNAME beside a MOVEMENT number, and the
original year in ORIGINALYEAR beside ORIGINALDATE, so an editor writing
back the values it showed erased them. An APE year write removed Date
in the same way.

Now only a clear removes the fallback. Writing back the value already
read changes nothing. A new value also replaces an existing fallback:
the name in MOVEMENTNAME, the four-digit year in ORIGINALYEAR, and the
year in Date.
The movement remarks now name MVNM and MVIN for ID3v2. The changelog
records the new ID3v2 MVNM and MVIN writes, the TDOR and TORY clear,
and the narrowed Vorbis and APE fallback rule.
Clearing the total from MVIN "2/5" must leave TXXX:MOVEMENTNUMBER
holding 2, so a reader that only looks at TXXX still sees the number.
A single-value getter returns the first name of a multi-value ID3v2
frame, APE item, or repeated Vorbis field, and the setter stored that
one name in place of the list. An editor that writes back what it read
erased every other artist, composer, or genre.

Every ID3v2 text and TXXX property, every APE text property, and every
Vorbis text field now leaves the tag alone when the value equals what
the getter returns, an empty string included. The ID3v2 movement
number, total, and original release date follow the same rule. Any
other value still replaces the list, and null still clears it.

An empty MOVEMENT, ORIGINALDATE, APE Year, or ID3v2 MVNM no longer
hides a filled fallback, and a TDOR or TORY without a date no longer
hides the other frame. Before, the getter returned the empty value and
writing it back deleted the fallback. An ID3v2 genre whose first value
is empty reads as an empty string, like the other text frames.
The write-back no-op compared the copied value with the target's
first value, so a copy into a Picard-style tag left the target's other
labels, ISRCs, catalog numbers, artists, and genres in place. CopyTo now
turns the no-op off on its thread while it writes, which also covers
tags a wrapper forwards to, so the target holds exactly the source
value. The rule lives in one place, Tag.IsWriteBack.

An empty string counted as a write-back whenever the getter read "",
so "" no longer cleared an ID3v2 frame or Vorbis field whose first value
is empty and whose later values hold text. It is now a write-back only
when no value holds text, which is when the APE getter reads "" too.

The ID3v2 year, podcast feed URL, and MusicBrainz recording ID setters
get the same guard, so a v2.3 year write-back no longer rewrites TYER
as UTF-8. A case-only edit and a null over a field that reads null are
pinned as writes.
A copy of the value a Vorbis movement or original date already reads
rewrote MOVEMENTNAME or ORIGINALYEAR to it, so copying one Picard tag
into another set MOVEMENTNAME to the movement number. An APE year copy
in the same way cut Date down to the year. The fallback is its own field,
so a copy of the value read leaves it, as it did before the write-back
rule, and only drops the other values of the primary field.
A single-value getter read an empty first value as "", so writing it
back counted as a clear and deleted the frame or field. ID3v2.4 stores
several strings as a NUL-separated list and Vorbis field names repeat,
so the getters now return the first non-empty value, and "" only when
every value is empty. A write-back of what was read keeps the frame,
and "" still clears it. This replaces reading a leading NUL as an empty
first value.

CopyTo now reads every source value before it writes any, skips a list
the target already holds, and treats a copy from the target itself, or
from a view that reads from it such as Mp3File.Tag or FlacFile.Tag, as
a write-back. Before, the first write cut the lists the copy then read,
so a file's view copied into its own tag kept only the first artist,
genre, label, and catalog number.

A copy of the original date a v2.3 tag already reads no longer drops
the TDOR full date beside TORY. A Vorbis or APE movement laid out as
Picard writes it, with the number in MOVEMENT beside MOVEMENTNAME,
reads and writes the name in MOVEMENTNAME and the number in MOVEMENT,
so a copy no longer writes the number over an ID3v2 MVNM. The ID3v2
comment and lyrics setters get the write-back guard, and the byte
identity test now uses foreign frame layouts.
Mp3File.Tag builds a new combined view on every read, so a copy from
one read into another was not seen as a copy between views over the
same tag, and its single-value lists were cut. Tags now expose the
tags they read from, and a copy between two that share one skips every
value the target already shows. A copy with Extended carries the
artist and genre in their lists, so a list the target holds keeps its
frames.

The ID3v2 comment and lyrics read the first non-empty frame, as text
frames do, so a write-back of what is shown changes nothing and ""
clears the frames without a description.

Vorbis and APE movements follow Picard's tag mapping: the name is
MOVEMENTNAME and the number is MOVEMENT. A MOVEMENT that is not a number
is still read as the name when no MOVEMENTNAME exists. Clearing one half
no longer changes how the other half reads.
A tag that holds one name took the first entry of a copied list even
when it was empty, so a default copy of ARTIST= plus ARTIST=V2 into
MP4, ASF, RIFF INFO, or ID3v1 cleared the artist and genre and dropped
the target's old ones. The base list setters and RIFF INFO's now keep
the first non-empty entry, the one the source shows.

Also pin the lyrics fallback to a described frame.
AiffTag reads the file's ID3 tag, but it did not name it, so a copy
between aiff.Tag and aiff.Id3v2Tag ran as a copy between unrelated tags
and cut TPUB and the other single-value lists. It now lists that tag in
Leaves, and a test checks every Tag type that holds another tag or a
file of tags.

The ID3v2.4 list reading trimmed every value and dropped blank ones,
while the single value kept them, so a Basic copy and the default copy
stored different names for " \0B". A NUL-separated value is now kept as
written and only an empty one is skipped, as other readers show it.
@decriptor
decriptor force-pushed the fix/id3-trailing-nul branch from 203bbbf to 0477a14 Compare November 22, 2026 02:17
The view guard looked for fields of a few shapes, so a view holding its
tags in an IEnumerable, a delegate, a base class's private field, or a
non-public holder member passed. Every concrete Tag type must now be a
listed leaf format tag or override Leaves, so a new type fails until
someone decides which it is.
…name

A subclass of a view, or a type that inherits a Leaves written for its
base, could add a tag of its own and still pass, and so could a type
reusing a leaf's simple name. Each view must now declare Leaves itself,
leaf tags are listed by full name, and eleven dummy views, one per
shape, pin the guard. The GetTextFrameValues remark no longer says the
first entry differs for a plain value.
TagTypeClassification holds the leaf list, the view and leaf rules, and
the library's view and unclassified lists, so other checks can use the
same rule as the guard.
@decriptor

Copy link
Copy Markdown
Owner Author

Review: 0 findings at 2c5b459.

What was checked:

  • Round 9 approved 58cc3d5. Its one minor and one trivial finding are fixed at 4fe8724, verified directly:
    • The Tag-type guard now treats a type as a view only when it declares Leaves itself, and lists leaf tags by full name.
    • All 11 view shapes the reviewers built are compiled into the suite and flagged: plain field, IEnumerable<Tag>, private field in a base, delegate, holder, primary constructor, IMediaFile, dictionary, CombinedTag subclass, inherited self-only Leaves, and a leaf's simple name reused in another namespace.
    • Reverting to "any override counts", or to matching leaf tags by simple name, turns the new test red.
    • The GetTextFrameValues doc now states exactly when its first entry matches GetTextFrame.
  • 2c5b459 moves that classification into a shared test helper so other tests can reuse it. The logic is unchanged.
  • Earlier rounds:
    • every round-1 to round-8 grid, including the 3,392-row list-to-single copy sweep, gives the same answers at 0477a14 and 58cc3d5;
    • all of rounds 1–8's findings are fixed and pinned.
  • Release build: 0 warnings on all 4 TFMs. net8.0 and net10.0: 4813 passed / 13 skipped. Format clean.
  • The branch merges cleanly into main ebd6138. CI is 8/8 green, including codecov.
  • Cross-PR note: fix: Read MP4 gnre and keep unedited ilst atoms #133 adds AudibleTag : Tag. Whichever of fix: Read MP4 gnre and keep unedited ilst atoms #133 and fix: Drop a terminating NUL from ID3 text #136 merges second must add it to LeafTagTypeNames, or the guard fails.

@decriptor decriptor added the approved AI has reviewed and signed off label Nov 22, 2026
@decriptor
decriptor merged commit 60008e0 into main Nov 22, 2026
8 checks passed
@decriptor
decriptor deleted the fix/id3-trailing-nul branch November 22, 2026 05:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved AI has reviewed and signed off

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: ID3 text keeps a terminating NUL

1 participant