Skip to content

MP4: An atom with a 64-bit size is skipped 8 bytes too far #746

Closed

Description

@Fan4Metal

Reproducer

I tried this code (lofty 0.25.4, also present on main):

use lofty::config::ParseOptions;
use lofty::file::FileType;
use lofty::prelude::*;
use lofty::probe::Probe;
use std::io::Cursor;

fn atom(ident: &[u8; 4], data: &[u8]) -> Vec<u8> {
    let mut v = ((data.len() + 8) as u32).to_be_bytes().to_vec();
    v.extend(ident);
    v.extend(data);
    v
}

fn main() {
    let mut meta = vec![0; 4]; // version + flags
    meta.extend(atom(b"hdlr", &[&[0; 8][..], b"mdirappl", &[0; 9]].concat()));
    meta.extend(atom(b"ilst", &atom(b"\xA9nam", &atom(b"data", b"\0\0\0\x01\0\0\0\0Title"))));
    let moov = atom(b"moov", &atom(b"udta", &atom(b"meta", &meta)));

    let mut file = atom(b"ftyp", b"isom\0\0\x02\0isomiso2mp41");
    // `mdat` with a 64-bit size (size field 1, then the size including the 16-byte header),
    // as written for any file over 4 GiB
    file.extend(1u32.to_be_bytes());
    file.extend(b"mdat");
    file.extend(24u64.to_be_bytes());
    file.extend([0; 8]);
    file.extend(moov);

    let result = Probe::with_file_type(Cursor::new(file), FileType::Mp4)
        .options(ParseOptions::new().read_properties(false))
        .read();
    match result {
        Ok(tagged) => println!("title: {:?}", tagged.primary_tag().and_then(|t| t.title())),
        Err(e) => println!("error: {e} ({:?})", std::error::Error::source(&e)),
    }
}

Output:

error: failed to parse Mp4 file (Some("file does not contain a \"moov\" atom"))

Summary

MP4: an atom with a 64-bit size is skipped 8 bytes too far.

skip_atom (src/mp4/read/mod.rs) seeks len - 8 bytes from the end of the header for an extended atom, but the header of such an atom is 16 bytes long (AtomInfo::read has already consumed the 64-bit size), so the reader lands 8 bytes past the atom's end. In Moov::find this means that when a large mdat comes before moov, the reader ends up inside moov (at its first child), never sees the moov header, and the read fails with "file does not contain a "moov" atom".

In practice this hits every MP4 over 4 GiB that is not faststart: I ran into it with a 4.8 GB film whose tags and cover lofty could not read, while ffprobe and mpv read them fine.

With len - 16 in the extended branch (or len - atom.header_size()), the reproducer prints title: Some("Title").

Expected behavior

The tags of a file with a 64-bit-sized mdat before moov are read, as with a 32-bit-sized one.

Assets

The reproducer builds the file in memory; the real-world file is a copyrighted film, so it is not attached.

Note: the cause was tracked down with the help of an AI assistant; the reproducer and the one-line fix were checked by hand against 0.25.4.

Activity

  1. added this to the 0.25.5 milestone on Oct 13, 2026
  2. added
    bug
    on Oct 13, 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