Description: do not write ID3v2 tags to non-MPEG files id3v2 wrote an ID3v2 tag to whatever file it was given. As the tag is prepended to the file, doing so to a non-MPEG container (e.g. an MP4 video) lands the tag in front of the container header and corrupts the file (Debian bug #446427, cloned from #445694). . Before tagging, the leading bytes of the file are now checked against a conservative blocklist of well-known non-audio container signatures (ISO Media/MP4, Ogg, RIFF, FLAC, Matroska/WebM, ASF). A matching file is skipped with a diagnostic and a non-zero exit status. The list intentionally only rejects formats that are known not to be MPEG audio; a genuine MP3, which starts with either an "ID3" tag or an MPEG frame sync, is never affected, so no existing tagging workflow regresses. Author: Martin A. Godisch Bug-Debian: https://bugs.debian.org/446427 Forwarded: no Last-Update: 2026-06-19 --- a/id3v2.cpp +++ b/id3v2.cpp @@ -120,6 +120,39 @@ return buf; } +// Signatures of common container formats that are NOT MPEG audio but are +// occasionally passed to id3v2 by mistake. Prepending an ID3v2 tag to any of +// them corrupts the file, because the tag lands in front of the container +// header (Debian bug #446427). This list is deliberately a conservative +// blocklist of well-known non-audio magic numbers: it never rejects a genuine +// MP3 (which starts either with an "ID3" tag or an MPEG frame sync), it only +// declines formats that are known not to be MPEG audio. +struct container_magic +{ + const char *sig; // bytes to match + size_t off; // offset at which they must appear + size_t len; // number of bytes to compare + const char *name; // human-readable description for the diagnostic +}; + +static const char *blocked_container(const unsigned char *hdr, size_t n) +{ + static const struct container_magic table[] = + { + { "ftyp", 4, 4, "ISO Media (MP4/MOV/3GP)" }, + { "OggS", 0, 4, "Ogg" }, + { "RIFF", 0, 4, "RIFF (WAV/AVI)" }, + { "fLaC", 0, 4, "FLAC" }, + { "\x1a\x45\xdf\xa3", 0, 4, "Matroska/WebM" }, + { "\x30\x26\xb2\x75", 0, 4, "ASF (WMA/WMV)" }, + }; + for (size_t i = 0; i < sizeof(table) / sizeof(table[0]); i++) + if (n >= table[i].off + table[i].len && + memcmp(hdr + table[i].off, table[i].sig, table[i].len) == 0) + return table[i].name; + return NULL; +} + int main( int argc, char *argv[]) { int iOpt; @@ -389,6 +422,20 @@ rv = 1; continue; } + + // Refuse to tag files that are clearly not MPEG audio: prepending an + // ID3v2 tag to such a container would corrupt it (Debian #446427). + unsigned char hdr[12]; + size_t hdrlen = fread(hdr, 1, sizeof(hdr), fp); + const char *container = blocked_container(hdr, hdrlen); + if (container != NULL) + { + std::cerr << "Refusing to tag '" << argv[nIndex] << "': " + << container << ", not MPEG audio\n"; + fclose(fp); + rv = 1; + continue; + } fclose(fp); myTag.Link(argv[nIndex], UpdFlags);