Skip to content

Inconsistent format detection: read_from_path succeeds but TagType::remove_from_path fails with "No format could be determined" on large ID3v2→first-frame gap #664

Closed

Description

@ad-si

Summary

For an MP3 with an ID3v2 header followed by a >1022-byte gap before the first
MPEG frame, read_from_path reads the file fine (and reports its tags), but
TagType::Id3v1.remove_from_path on the same file fails with:

No format could be determined from the provided file

The read path and the tag-mutation path disagree on whether the file is a
recognizable MPEG file. If a file reads as MPEG, removing a tag from it
shouldn't fail format detection.

Version

lofty 0.24.0 (also reproduces on 0.22.4)

Minimal reproduction (no external files)

use lofty::tag::TagType;
use lofty::file::TaggedFileExt;

// ID3v2.3 header (empty body) + `gap` zero bytes + a few MPEG-1 Layer III
// frames (FF FB 90 00, len 417) + a 128-byte ID3v1 tag.
fn build(gap: usize) -> Vec<u8> {
    let mut v = Vec::new();
    v.extend_from_slice(b"ID3");
    v.extend_from_slice(&[3, 0, 0, 0, 0, 0, 0]); // syncsafe size = 0
    v.extend(std::iter::repeat(0u8).take(gap));
    for _ in 0..8 {
        v.extend_from_slice(&[0xFF, 0xFB, 0x90, 0x00]);
        v.extend(std::iter::repeat(0u8).take(417 - 4));
    }
    let mut t = vec![0u8; 128];
    t[..3].copy_from_slice(b"TAG");
    v.extend_from_slice(&t);
    v
}

fn main() {
    for gap in [0usize, 1022, 1023, 2000] {
        let p = format!("/tmp/repro_{gap}.mp3");
        std::fs::write(&p, build(gap)).unwrap();
        let read = lofty::read_from_path(&p).map(|t| t.tags().len());
        let rm = match TagType::Id3v1.remove_from_path(&p) {
            Ok(()) => "OK".to_string(),
            Err(e) => format!("ERR: {e}"),
        };
        println!("gap={gap:>4}: read={read:?}  remove_id3v1={rm}");
    }
}

Output

gap=   0: read=Ok(2)  remove_id3v1=OK
gap=1022: read=Ok(2)  remove_id3v1=OK
gap=1023: read=Ok(2)  remove_id3v1=ERR: No format could be determined from the provided file
gap=2000: read=Ok(2)  remove_id3v1=ERR: No format could be determined from the provided file

Expected vs actual

  • Expected: if read_from_path recognizes the file as MPEG and lists its
    tags, remove_from_path should also recognize it (and remove/no-op the tag).
  • Actual: the mutation path's format detection appears to scan only ~1024
    bytes for a frame sync, so a large gap between the ID3v2 tag and the first
    frame makes it return UnknownFormat, while the read path tolerates it.

Context

Encountered with a real-world file (a commercially distributed MP3) that has a
~1044-byte zero gap between its ID3v2 tag and the first audio frame. A GUI
tagger built on lofty surfaced it as "Error deleting ID3v1: No format could be
determined from the provided file", even though the file reads and plays fine.

Suggested fix

Make the mutation/removal path's format detection consistent with the read
path — e.g. when an ID3v2 header is present, treat the file as MPEG (or extend
the frame-sync search past the ID3v2 boundary) rather than bailing with
UnknownFormat.

Activity

  1. Serial-ATA commented on Jun 10, 2026

    @Serial-ATA
    Owner

    Ah yeah, TagType::remove_from_path() will try to guess the format from the file content and lofty::read_from_path() will guess from the extension. Good catch!

  2. added
    bug
    on Jun 10, 2026
  3. added this to the 0.25.0 milestone on Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions