fix(library): drop the stray zero a tagger writes after every UTF-16 string, not only the last (#8060)
release / go (push) Successful in 2m0s
release / web (push) Successful in 1m10s
release / govulncheck (push) Successful in 21s
release / integration (push) Successful in 4m10s
release / android (push) Successful in 4m56s
release / Build signed APK (push) Successful in 5m13s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 15s
release / Verify release artifacts (tag releases only) (push) Skipped

The first repair trimmed one stray zero at the end of a UTF-16 frame. The
same tagger writes one after every string, so a custom field (TXXX) such
as ARTISTS or REPLAYGAIN carries one between its name and its value as
well, and dhowden/tag still refused those files. Zeros between a
terminator and the next byte order mark, or the end of the frame, are now
dropped too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 15:52:03 -05:00
co-authored by Claude Opus 5.5
parent c9e058b3c9
commit 1b13aa8b7f
2 changed files with 81 additions and 17 deletions
+52 -17
View File
@@ -1,10 +1,10 @@
// Package tagread reads a music file's tags. It is github.com/dhowden/tag // Package tagread reads a music file's tags. It is github.com/dhowden/tag
// with one repair: dhowden/tag refuses a whole file when a single UTF-16 text // with one repair: dhowden/tag refuses a whole file when a single UTF-16 text
// frame holds an odd number of bytes, and some taggers end a UTF-16 frame // frame holds an odd number of bytes, and some taggers write one zero byte
// with one zero byte too many (a two-byte terminator followed by a stray // too many after each UTF-16 string in a frame. Those files then read as
// single one). Those files then read as having no tags at all, and the // having no tags at all, and the scanner skipped them. When dhowden/tag
// scanner skipped them. When dhowden/tag fails, ReadFrom rebuilds the ID3v2 // fails, ReadFrom rebuilds the ID3v2 tag in memory without the stray bytes
// tag in memory without the stray byte and reads it again. // and reads it again.
package tagread package tagread
import ( import (
@@ -16,8 +16,8 @@ import (
) )
// ReadFrom reads rs's tags, as tag.ReadFrom does. When that fails on an ID3v2 // ReadFrom reads rs's tags, as tag.ReadFrom does. When that fails on an ID3v2
// tag with a stray trailing zero in a UTF-16 text frame, it answers from a // tag with stray zero bytes in its UTF-16 text frames, it answers from a
// copy of the tag with that byte removed. Any other failure returns // copy of the tag with those bytes removed. Any other failure returns
// tag.ReadFrom's own error. // tag.ReadFrom's own error.
func ReadFrom(rs io.ReadSeeker) (tag.Metadata, error) { func ReadFrom(rs io.ReadSeeker) (tag.Metadata, error) {
start, err := rs.Seek(0, io.SeekCurrent) start, err := rs.Seek(0, io.SeekCurrent)
@@ -43,7 +43,8 @@ func ReadFrom(rs io.ReadSeeker) (tag.Metadata, error) {
} }
// repairID3v2 reads the ID3v2 tag at rs's position and returns it rebuilt // repairID3v2 reads the ID3v2 tag at rs's position and returns it rebuilt
// with each stray trailing zero trimmed (trimStrayNul). It reports false // with the stray zeros removed from each UTF-16 text frame
// (dropStrayZeros). It reports false
// when there is no ID3v2.3 or 2.4 tag, the tag can't be walked safely, or // when there is no ID3v2.3 or 2.4 tag, the tag can't be walked safely, or
// nothing needed trimming, so the caller keeps the original error. // nothing needed trimming, so the caller keeps the original error.
// //
@@ -90,8 +91,8 @@ func repairID3v2(rs io.Reader) ([]byte, bool) {
frameFlags := frames[off+8 : off+10] frameFlags := frames[off+8 : off+10]
data := frames[off+10 : off+10+n] data := frames[off+10 : off+10+n]
if frameFlags[0] == 0 && frameFlags[1] == 0 { if frameFlags[0] == 0 && frameFlags[1] == 0 {
if trimmed, ok := trimStrayNul(id, data); ok { if repaired, ok := dropStrayZeros(id, data); ok {
data, changed = trimmed, true data, changed = repaired, true
} }
} }
out = append(out, frames[off:off+4]...) out = append(out, frames[off:off+4]...)
@@ -111,10 +112,18 @@ func repairID3v2(rs io.Reader) ([]byte, bool) {
return append(tagBytes, out...), true return append(tagBytes, out...), true
} }
// trimStrayNul drops the last byte of a UTF-16 text frame whose text runs // dropStrayZeros removes the zero bytes a faulty tagger writes after each
// to an odd number of bytes and ends in zero: the stray terminator. A UTF-16 // string of a UTF-16 text frame, and reports whether there were any. That
// frame of odd length that ends any other way is left alone. // tagger writes every string as its byte order mark, its text, its
func trimStrayNul(id string, data []byte) ([]byte, bool) { // terminator when it has one, and then one extra zero. The extra zero puts
// everything after it out of step with the two-byte characters, and leaves
// the frame an odd length.
//
// Only two places are touched, both of which a well-formed frame never has:
// zeros between a string's terminator and the next string's byte order mark
// (or the end of the frame), and a lone zero left over at the very end. A
// frame of odd length that ends in any other byte is left alone.
func dropStrayZeros(id string, data []byte) ([]byte, bool) {
textStart := 1 // the encoding byte textStart := 1 // the encoding byte
switch { switch {
case id == "COMM" || id == "USLT": case id == "COMM" || id == "USLT":
@@ -125,10 +134,36 @@ func trimStrayNul(id string, data []byte) ([]byte, bool) {
if len(data) <= textStart || (data[0] != 1 && data[0] != 2) { if len(data) <= textStart || (data[0] != 1 && data[0] != 2) {
return nil, false return nil, false
} }
if (len(data)-textStart)%2 == 0 || data[len(data)-1] != 0 { text := data[textStart:]
return nil, false out := append(make([]byte, 0, len(data)), data[:textStart]...)
changed := false
i := 0
for i+1 < len(text) {
out = append(out, text[i], text[i+1])
if text[i] == 0 && text[i+1] == 0 {
next := i + 2
for next < len(text) && text[next] == 0 {
next++
}
if next > i+2 && (next == len(text) || isBOM(text[next:])) {
i, changed = next, true
continue
}
}
i += 2
} }
return data[:len(data)-1], true if i < len(text) {
if text[i] != 0 {
return nil, false
}
changed = true
}
return out, changed
}
// isBOM reports whether b starts with a UTF-16 byte order mark.
func isBOM(b []byte) bool {
return len(b) >= 2 && (b[0] == 0xFE && b[1] == 0xFF || b[0] == 0xFF && b[1] == 0xFE)
} }
// appendFrameSize writes a frame's length field: syncsafe in 2.4, plain // appendFrameSize writes a frame's length field: syncsafe in 2.4, plain
+29
View File
@@ -60,6 +60,35 @@ func TestReadFrom_TrimsAStrayZeroAfterAUTF16Frame(t *testing.T) {
} }
} }
// The same tagger writes a custom (TXXX) field as its name and its value,
// each with a stray zero after it: one sits between the name's terminator and
// the value's byte order mark, the other at the end of the frame.
func TestReadFrom_DropsTheStrayZeroAfterEachStringOfACustomField(t *testing.T) {
name := append(utf16BE("ARTISTS"), 0)
value := utf16BE("Saint Motel")
value = append(value[1:len(value)-2], 0) // no encoding byte, no terminator, a stray zero
strayField := append(name, value...)
file := id3(4,
frame(4, "TXXX", strayField),
frame(4, "TCON", append(utf16BE("Pop"), 0)),
frame(4, "TIT2", utf8Text("Fine Wine")),
)
if _, err := tag.ReadFrom(bytes.NewReader(file)); err == nil {
t.Fatal("dhowden/tag read the fixture; it no longer reproduces the fault")
}
meta, err := ReadFrom(bytes.NewReader(file))
if err != nil {
t.Fatalf("ReadFrom: %v", err)
}
field, ok := meta.Raw()["TXXX"].(*tag.Comm)
if !ok || field.Description != "ARTISTS" || field.Text != "Saint Motel" {
t.Fatalf("TXXX = %#v", meta.Raw()["TXXX"])
}
if meta.Genre() != "Pop" || meta.Title() != "Fine Wine" {
t.Fatalf("genre %q title %q", meta.Genre(), meta.Title())
}
}
func TestReadFrom_LeavesOtherFailuresAlone(t *testing.T) { func TestReadFrom_LeavesOtherFailuresAlone(t *testing.T) {
// Odd-length UTF-16 that doesn't end in zero is not the stray // Odd-length UTF-16 that doesn't end in zero is not the stray
// terminator, so it is not guessed at. // terminator, so it is not guessed at.