Skip to content

MP4: Writing a tag fails with "failed to fill whole buffer" when the file has a co64 atom #754

Closed

Description

@shannonbay

Reproducer

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

use lofty::config::{ParseOptions, WriteOptions};
use lofty::file::FileType;
use lofty::prelude::*;
use lofty::probe::Probe;
use lofty::tag::{Tag, TagType};

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() {
    // `co64` with a normal 32-bit atom size: version/flags, entry count 1, one 64-bit offset
    let mut co64 = vec![0; 4];
    co64.extend(1u32.to_be_bytes());
    co64.extend(96u64.to_be_bytes()); // start of `mdat`'s data below (after `moov`, so it must shift)
    let stbl = atom(b"stbl", &atom(b"co64", &co64));
    let moov = atom(b"moov", &atom(b"trak", &atom(b"mdia", &atom(b"minf", &stbl))));

    let mut file = atom(b"ftyp", b"M4A \0\0\0\0M4A isom");
    file.extend(moov);
    file.extend(atom(b"mdat", &[0; 16]));

    let path = std::env::temp_dir().join("lofty-co64-repro.m4a");
    std::fs::write(&path, &file).unwrap();

    let mut tag = Tag::new(TagType::Mp4Ilst);
    tag.set_title("Title".into());
    match tag.save_to_path(&path, WriteOptions::default()) {
        Ok(()) => {
            let tagged = Probe::open(&path)
                .unwrap()
                .set_file_type(FileType::Mp4)
                .options(ParseOptions::new().read_properties(false))
                .read()
                .unwrap();
            println!("ok, title: {:?}", tagged.primary_tag().and_then(|t| t.title()));
            // Check the chunk offset still points at the start of mdat's data
            let out = std::fs::read(&path).unwrap();
            let co = out.windows(4).position(|w| w == b"co64").unwrap();
            let off = u64::from_be_bytes(out[co + 12..co + 20].try_into().unwrap());
            let mdat = out.windows(4).position(|w| w == b"mdat").unwrap();
            println!("co64 offset {off}, mdat data starts at {}", mdat + 4);
        }
        Err(e) => println!("error: {e} ({:?})", std::error::Error::source(&e).map(|s| s.to_string())),
    }
}

Output:

error: failed to write Mp4 file (Some("failed to fill whole buffer"))

Summary

MP4: writing a tag fails with "failed to fill whole buffer" when the file has a co64 atom.

In update_offsets (src/mp4/write.rs), the 64-bit offsets loop seeks to the entry count with:

self.seek(SeekFrom::Start(co64_start + ATOM_HEADER_LEN + 8 + 4))?;

The extra + 8 treats every co64 as if it had a 64-bit atom size, but co64 only holds 64-bit offsets; its own header is normally the usual 8 bytes, the same as stco (whose loop correctly uses stco_start + ATOM_HEADER_LEN + 4). So the entry count is read from the wrong place (here, the low 32 bits of the first offset, i.e. 96) and the loop runs past the end of the buffer.

Using the atom's real header size fixes it:

self.seek(SeekFrom::Start(co64_start + co64.header_size() + 4))?;

With that change the reproducer prints:

ok, title: Some("Title")
co64 offset 186, mdat data starts at 186

I hit this together with #746 on 3GP voice recordings from a phone (ftyp brand 3gp4, a 64-bit-sized mdat before moov, and co64 chunk offsets): once #746 is fixed they read fine, but writing tags still failed with this error.

Expected behavior

The tag is written and the co64 chunk offsets are shifted by the size change, as already happens for stco.

Assets

The reproducer builds the file in memory. The real-world files are personal voice recordings, so they aren't attached.

Note: the cause was tracked down with the help of an AI assistant; the reproducer and the one-line fix were run against 0.25.4 from crates.io (fails) and with the fix applied (passes, offsets correct).

Activity

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