fix(library): a UTF-16 tag frame with a stray trailing zero no longer hides the whole file (#8060)
release / go (push) Successful in 2m4s
release / web (push) Successful in 1m21s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m9s
release / android (push) Successful in 4m28s
release / Build signed APK (push) Successful in 4m56s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 14s
release / Verify release artifacts (tag releases only) (push) Skipped
release / go (push) Successful in 2m4s
release / web (push) Successful in 1m21s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m9s
release / android (push) Successful in 4m28s
release / Build signed APK (push) Successful in 4m56s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 14s
release / Verify release artifacts (tag releases only) (push) Skipped
Some taggers end a UTF-16 text frame with a two-byte terminator and then one more zero byte, so the frame holds an odd number of bytes. dhowden/tag refuses the whole tag when it meets one, and the scanner then skipped the file: 61 tracks on dev (whole albums from one tagger, with a genre or year frame written this way) never reached the library. internal/tagread.ReadFrom reads with dhowden/tag first. When that fails on an ID3v2.3/2.4 tag, it rebuilds the tag in memory with the stray byte trimmed from each such frame and reads it again. Anything else still fails with dhowden/tag's own error. The scanner, both MBID backfills, embedded cover art and Subsonic lyrics all read through it. The ID3v2 primitives (syncsafe and frame sizes, the extended header, unsynchronisation) move from library/id3v2genre.go into tagread, so the genre reader and the repair share one copy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
"io"
|
||||
"strings"
|
||||
"unicode/utf16"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/tagread"
|
||||
)
|
||||
|
||||
// Why this file exists at all: github.com/dhowden/tag reads every other field
|
||||
@@ -27,12 +29,6 @@ import (
|
||||
// So the TCON frame is parsed here directly. Only the genre frame — everything
|
||||
// else still comes from dhowden/tag, which handles it fine.
|
||||
|
||||
// maxID3TagSize caps how much of a file we'll buffer looking for TCON. Real
|
||||
// tags are kilobytes; embedded cover art pushes them to a few megabytes. The
|
||||
// cap exists so a corrupt or hostile size field can't make the scanner
|
||||
// allocate wildly on a file it was only asked to index.
|
||||
const maxID3TagSize = 16 << 20
|
||||
|
||||
// errNoGenreFrame means the file carries no readable genre frame. It is an
|
||||
// expected outcome (plenty of files are untagged), not a failure.
|
||||
var errNoGenreFrame = errors.New("library: no ID3v2 genre frame")
|
||||
@@ -62,8 +58,8 @@ func readID3v2GenreValues(rs io.ReadSeeker) ([]string, error) {
|
||||
return nil, errNoGenreFrame
|
||||
}
|
||||
tagFlags := hdr[5]
|
||||
size := syncsafeInt(hdr[6:10])
|
||||
if size <= 0 || size > maxID3TagSize {
|
||||
size := tagread.SyncsafeInt(hdr[6:10])
|
||||
if size <= 0 || size > tagread.MaxTagSize {
|
||||
return nil, errNoGenreFrame
|
||||
}
|
||||
|
||||
@@ -83,11 +79,11 @@ func readID3v2GenreValues(rs io.ReadSeeker) ([]string, error) {
|
||||
// Whole-tag unsynchronisation (2.2/2.3). 2.4 moved this per-frame, but
|
||||
// some writers still set it at tag level, and undoing it twice is
|
||||
// harmless: after the first pass no 0xFF 0x00 pairs remain.
|
||||
body = undoUnsynchronisation(body)
|
||||
body = tagread.UndoUnsynchronisation(body)
|
||||
}
|
||||
if major >= 3 && tagFlags&0x40 != 0 {
|
||||
var ok bool
|
||||
if body, ok = skipExtendedHeader(body, major); !ok {
|
||||
if body, ok = tagread.SkipExtendedHeader(body, major); !ok {
|
||||
return nil, errNoGenreFrame
|
||||
}
|
||||
}
|
||||
@@ -99,7 +95,7 @@ func readID3v2GenreValues(rs io.ReadSeeker) ([]string, error) {
|
||||
func findGenreFrame(body []byte, major byte) ([]string, error) {
|
||||
// 2.2 frames: 3-byte id + 3-byte size, no flags. 2.3/2.4: 4-byte id +
|
||||
// 4-byte size + 2-byte flags. The size field is the other difference that
|
||||
// matters — see frameSize.
|
||||
// matters — see tagread.FrameSize.
|
||||
idLen, sizeLen, flagLen := 4, 4, 2
|
||||
wantID := "TCON"
|
||||
if major == 2 {
|
||||
@@ -115,7 +111,7 @@ func findGenreFrame(body []byte, major byte) ([]string, error) {
|
||||
if body[off] == 0 {
|
||||
break
|
||||
}
|
||||
size := frameSize(body[off+idLen:off+idLen+sizeLen], major)
|
||||
size := tagread.FrameSize(body[off+idLen:off+idLen+sizeLen], major)
|
||||
if size <= 0 || off+hdrLen+size > len(body) {
|
||||
// Bogus length — we can't trust any offset past this point.
|
||||
break
|
||||
@@ -136,25 +132,6 @@ func findGenreFrame(body []byte, major byte) ([]string, error) {
|
||||
return nil, errNoGenreFrame
|
||||
}
|
||||
|
||||
// frameSize decodes a frame's length field. 2.4 made it syncsafe (7 bits per
|
||||
// byte); 2.2 and 2.3 are plain big-endian. Reading a 2.3 size as syncsafe (or
|
||||
// the reverse) yields a plausible-looking wrong offset rather than an obvious
|
||||
// error, which is exactly how frame-walking bugs go unnoticed.
|
||||
func frameSize(b []byte, major byte) int {
|
||||
switch major {
|
||||
case 2:
|
||||
return int(b[0])<<16 | int(b[1])<<8 | int(b[2])
|
||||
case 3:
|
||||
n := binary.BigEndian.Uint32(b)
|
||||
if n > maxID3TagSize {
|
||||
return -1
|
||||
}
|
||||
return int(n)
|
||||
default:
|
||||
return syncsafeInt(b)
|
||||
}
|
||||
}
|
||||
|
||||
// frameData strips per-frame wrappers and reports whether the payload is
|
||||
// readable at all. Compressed and encrypted frames are not (we have no
|
||||
// zlib-in-frame or key handling, and neither is meaningful for a genre tag).
|
||||
@@ -192,7 +169,7 @@ func frameData(data []byte, major byte, flags uint16) ([]byte, bool) {
|
||||
data = data[4:] // syncsafe expanded size; we don't need it
|
||||
}
|
||||
if flags&0x0002 != 0 {
|
||||
data = undoUnsynchronisation(data)
|
||||
data = tagread.UndoUnsynchronisation(data)
|
||||
}
|
||||
return data, true
|
||||
}
|
||||
@@ -333,56 +310,3 @@ func decodeUTF16(b []byte, bigEndian bool) string {
|
||||
}
|
||||
return string(utf16.Decode(units))
|
||||
}
|
||||
|
||||
// skipExtendedHeader advances past the optional extended header. The two
|
||||
// versions disagree about whether the size field counts itself, which is worth
|
||||
// spelling out because getting it wrong offsets the entire frame list by four
|
||||
// bytes and makes every frame id look like padding.
|
||||
func skipExtendedHeader(body []byte, major byte) ([]byte, bool) {
|
||||
if len(body) < 4 {
|
||||
return nil, false
|
||||
}
|
||||
if major == 3 {
|
||||
// 2.3: size EXCLUDES the four size bytes themselves.
|
||||
size := int(binary.BigEndian.Uint32(body[0:4]))
|
||||
if size < 0 || 4+size > len(body) {
|
||||
return nil, false
|
||||
}
|
||||
return body[4+size:], true
|
||||
}
|
||||
// 2.4: syncsafe size INCLUDING the size bytes.
|
||||
size := syncsafeInt(body[0:4])
|
||||
if size < 4 || size > len(body) {
|
||||
return nil, false
|
||||
}
|
||||
return body[size:], true
|
||||
}
|
||||
|
||||
// syncsafeInt decodes a 4-byte synchsafe integer (7 significant bits per byte).
|
||||
func syncsafeInt(b []byte) int {
|
||||
if len(b) < 4 {
|
||||
return -1
|
||||
}
|
||||
// A set high bit means this isn't a valid synchsafe integer. Some writers
|
||||
// emit a plain big-endian size here; refusing is safer than silently
|
||||
// dropping bits and walking to a wrong offset.
|
||||
for _, c := range b[:4] {
|
||||
if c&0x80 != 0 {
|
||||
return -1
|
||||
}
|
||||
}
|
||||
return int(b[0])<<21 | int(b[1])<<14 | int(b[2])<<7 | int(b[3])
|
||||
}
|
||||
|
||||
// undoUnsynchronisation collapses the 0xFF 0x00 pairs that unsynchronisation
|
||||
// inserts to stop a tag from looking like an MPEG frame sync.
|
||||
func undoUnsynchronisation(b []byte) []byte {
|
||||
out := make([]byte, 0, len(b))
|
||||
for i := 0; i < len(b); i++ {
|
||||
out = append(out, b[i])
|
||||
if b[i] == 0xFF && i+1 < len(b) && b[i+1] == 0x00 {
|
||||
i++
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -6,11 +6,11 @@ import (
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"github.com/dhowden/tag"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/tagread"
|
||||
)
|
||||
|
||||
// BackfillMBIDsResult tallies what the one-shot worker did.
|
||||
@@ -147,7 +147,7 @@ func readMBIDsForFile(path string, logger *slog.Logger) (albumMBID, artistMBID s
|
||||
}
|
||||
defer func() { _ = f.Close() }()
|
||||
|
||||
meta, err := tag.ReadFrom(f)
|
||||
meta, err := tagread.ReadFrom(f)
|
||||
if err != nil {
|
||||
logger.Warn("mbid backfill: tag read failed", "path", path, "err", err)
|
||||
return "", ""
|
||||
@@ -241,7 +241,7 @@ func readRecordingMBIDForFile(path string, logger *slog.Logger) string {
|
||||
}
|
||||
defer func() { _ = f.Close() }()
|
||||
|
||||
meta, err := tag.ReadFrom(f)
|
||||
meta, err := tagread.ReadFrom(f)
|
||||
if err != nil {
|
||||
logger.Warn("track mbid backfill: tag read failed", "path", path, "err", err)
|
||||
return ""
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/dhowden/tag"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
@@ -23,6 +22,7 @@ import (
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
syncpkg "git.fabledsword.com/bvandeusen/minstrel/internal/sync"
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/tagread"
|
||||
)
|
||||
|
||||
// audioExtensions is the set the scanner indexes. Keep in sync with
|
||||
@@ -368,7 +368,7 @@ func (s *Scanner) scanFile(
|
||||
}
|
||||
defer func() { _ = f.Close() }()
|
||||
|
||||
meta, err := tag.ReadFrom(f)
|
||||
meta, err := tagread.ReadFrom(f)
|
||||
if err != nil {
|
||||
return pgtype.UUID{}, false, fmt.Errorf("tag read: %w", err)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/tagread"
|
||||
)
|
||||
|
||||
// Why this file exists: Vorbis comments (FLAC, Ogg Vorbis, Opus) say a field
|
||||
@@ -59,7 +61,7 @@ func readFLACValues(rs io.ReadSeeker, key string) ([]string, error) {
|
||||
if _, err := io.ReadFull(rs, rest[:]); err != nil {
|
||||
return nil, errNoGenreFrame
|
||||
}
|
||||
size := syncsafeInt(rest[2:6])
|
||||
size := tagread.SyncsafeInt(rest[2:6])
|
||||
if size < 0 {
|
||||
return nil, errNoGenreFrame
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user