Skip to content

OGG Vorbis: writing a tag silently drops all audio when the setup header ends mid-page #729

Closed

Description

@trougnouf

Summary

Writing Vorbis comments to an OGG file whose third packet (the Vorbis setup header) ends in the middle of an Ogg page, i.e. it shares a page with the first audio packets, makes lofty's OGG writer discard the entire audio stream and return Ok(()). The resulting file contains only the header packets. The data loss is silent: no error is raised, so the caller believes the write succeeded.
I have lost 36 ogg songs using currant with lofty-rs due to this issue.

Reproducer

lofty_ogg_bug_repro.zip

A minimal, self-contained reproduction is attached:

  • repro/ — a small cargo binary that depends only on lofty from crates.io.
    It copies a fixture, inserts a Popularimeter tag (as a music player would), and calls save_to_path.
  • repro/fixtures/fixture_trigger.ogg — a 3 s generated clip whose setup header ends mid-page (the layout that triggers the bug).
  • repro/fixtures/fixture_safe.ogg — a 3 s generated clip whose setup header ends exactly on a page boundary (the control).
  • run_repro.sh — builds the repro and runs it on both fixtures.

sh run_repro.sh output (lofty 0.25.4):

########## TRIGGER (setup header ends mid-page) ##########
input : 8403 bytes, 4 pages
save_to_path -> Ok(())  (tag_inserted=true)
output: 3673 bytes, 3 pages
re-parse: duration=0ns
VERDICT: BUG REPRODUCED -- save_to_path returned Ok, but pages dropped 4 -> 3;
         the audio stream was silently discarded.

########## SAFE (control: setup header ends on page boundary) ##########
input : 8430 bytes, 5 pages
save_to_path -> Ok(())  (tag_inserted=true)
output: 8481 bytes, 6 pages
re-parse: duration=3s
VERDICT: OK -- page count preserved (5 -> 6); audio intact.

The trigger file decodes correctly before the write (ffprobe / libvorbis report 3.0 s); after the write it is a 0 s, header-only file.

Root cause

In src/ogg/tag/write.rs, the writer reads the Vorbis header packets at packet granularity, then appends the "rest of the file" parsed as pages:

// write.rs:68-69
let mut packets =
    Packets::read_count(&mut file, header_packet_count).map_err(FileParseError::from)?;

// write.rs:71-72
let mut remaining_file_content = Vec::new();
file.read_to_end(&mut remaining_file_content)?;

Packets::read_count (ogg_pager 0.7.2, src/packets.rs) stops as soon as it has read the requested number of packets. When the last header packet (the setup header, packet index 2 for Vorbis) ends mid-page, the reader breaks
out immediately after consuming that packet's final byte — it does not advance to the next page boundary. The file position is therefore left in the middle of a page.

read_to_end then captures a partial page (the tail of the page the setup header ended in) followed by the remaining whole pages. The writer later tries to re-emit that buffer as a sequence of pages:

// write.rs:123-132
let mut pages_reader = Cursor::new(&remaining_file_content[..]);
let mut idx = 0;
while let Ok(mut page) = Page::read(&mut pages_reader) {
    let header = page.header_mut();
    header.sequence_number = pages_written + idx;
    page.gen_crc();
    file.write_all(&page.as_bytes())?;
    idx += 1;
}

The first "page" in the buffer starts mid-page, so its first four bytes are not the OggS magic. Page::read errors, the while let Ok(..) loop exits immediately, and no audio pages are written. The function then returns Ok(()) (write.rs:134), so the caller sees a successful write.

When the setup header happens to end exactly on a page boundary, the file position lands on a page start, Page::read succeeds, and the audio is preserved — which is why the bug is layout-dependent and easy to miss.

Why this layout is valid (and common)

An Ogg page may contain any number of packets, and a packet may span pages (RFC 3533). Nothing requires a Vorbis header packet to occupy a page by itself, so a page holding the setup header and the start of the audio is conformant. Decoders operate on packets, not pages, and decode these files correctly (ffprobe / libvorbis report the full 3.0 s for the trigger fixture).

This is not a rare corner case; a quarter of my OGG collection is affected (including all files coming from Jamendo and many obtained elsewhere) and would be reduced to a header-only file by a single tag write. Strict validators such as oggz flag the layout ("Terminal header page contains non-header packet"), but that does not make the file unplayable or non-conformant.

Impact

Silent, total loss of the audio stream on a routine tag write, reported as success. For a music player this means a track is destroyed the first time its metadata is updated (with e.g. play count or rating).

Expected behavior

No Ok(()) on catastrophic data loss.

AI usage: Investigated and solved with AI assistance (Qwen 3.8 Q4)

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