Skip to content

Decoding robustness: no Cluster, FLAC block size, buffered read - #559

Merged
JeromeMartinez merged 7 commits into
MediaArea:mainfrom
JeromeMartinez:Decoding_Robustness2
Nov 23, 2026
Merged

JeromeMartinez merged 7 commits into
MediaArea:mainfrom
JeromeMartinez:Decoding_Robustness2

Conversation

@JeromeMartinez

@JeromeMartinez JeromeMartinez commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Follow-up of #558, fixes found during its review.

  • Matroska without Cluster: in a file without any Cluster (e.g. all blocks removed by a remux), a WAV or AIFF file was not output and its missing audio data was not reported (exit code 0, --check OK if the file was encoded without --hash), and only the last missing DPX frame was listed. The init done at the first Cluster is now also done at the end of the parsing: the header and trailer stored in the reversibility data are output, the audio size check reports 0 bytes, and the first and last missing frames are listed.
  • FLAC: the buffer of the decoded samples was created for 16384 samples, then resized to the exact size of a block. A block of more than 16384 samples (up to 65535 in the FLAC format, e.g. from FFmpeg with -frame_size) or bigger than the previous blocks (e.g. 1152-sample blocks then 4608-sample blocks) was written after the end of the buffer (heap buffer overflow). The buffer is now created again when it is too small for the block.
  • Buffer: buffer_or_view::Resize() read after the end of the old buffer when the buffer grows (its only caller growing a buffer was the FLAC decoder), buffer::Resize() did not free the old buffer, and the unused CopyCut() is removed.
  • Buffered read (--io other than mmap): the base address of the reversibility data was not updated after a remap before the first Cluster (the file is read through a memory map until there whatever the method, it worked only when the new map was at the same address), and the file map of the clusters was never deleted, filemap::Close() handling a buffered read as a memory map (buffer given to munmap(), CloseHandle() on the buffered read info on Windows, 256 MiB not freed per file). When the open failed, the buffered read info, and the file stream or handle on some failures, were not freed.

The init of the outputs and checks (tracks, attachments with a size of 0,
hashes) was done only at the first Cluster. In a file without any Cluster
(e.g. all blocks removed by a remux), a WAV or AIFF file was not output and
its missing audio data was not reported (exit code 0, check OK if the file
was encoded without hashes), and only the last missing DPX frame was listed.

This init is now also done at the end of the parsing if there is no Cluster:
the parts of the files stored in the reversibility data are output (e.g. the
header of a WAV file), the audio size check reports 0 bytes, and the first
and last missing frames are listed. The reversibility data is still in the
buffer at this point, the whole file being mapped until the first Cluster.
The buffer of the decoded samples was created once for 16384 samples, then
resized by Resize() to the exact size of a block when a block had the same
size as the previous one. A block of more than 16384 samples (up to 65535 in
the FLAC format, e.g. from FFmpeg with -frame_size), or a block bigger than
the previous blocks after such a resize (e.g. 1152-sample blocks then
4608-sample blocks), was written after the end of the buffer (heap buffer
overflow). The buffer is now created again when it is too small for the
block.
buffer_or_view::Resize() copied NewSize bytes from the old buffer of Size()
bytes when the buffer grows, reading after its end. Its only caller growing
a buffer was the FLAC decoder, which wrote after the end of the buffer
before the resize (fixed in the previous commit). buffer::Resize() did not
free the old buffer when the buffer grows or when the new size is 0. Both
functions now keep the buffer if the size is the same, without a new
allocation.
The base address of the reversibility data was updated after a remap of the
file only with the memory map method, but until the first Cluster the file
is always read through a memory map of the whole file, whatever the method
set by --io (the buffered read is only for the clusters). With another
method, after a remap (1 MiB of content before the first Cluster), the
reversibility data kept the address of the released map, which worked only
when the new map was at the same address. The base address is now updated
when the remapped file map is the one of the reversibility data.
Close() handled a buffered read (--io other than mmap) as a memory map: the
buffer was given to munmap() (to UnmapViewOfFile() on Windows, with a
CloseHandle() on the buffered read info), and the buffer, the file and the
buffered read info were not freed. The file map of the clusters, the only
one with a buffered read, was never deleted (256 MiB not freed per Matroska
file). Close() now frees the buffer, closes the file and deletes the
buffered read info, and the file map of the clusters is deleted with the
Matroska parser.
It has no caller.
When the open of a file for a buffered read (--io other than mmap) failed,
the buffered read info was not freed, and neither was the file stream with
the fstream method nor the file handle on Windows when its size or the seek
failed. They are now freed on each failure.
@JeromeMartinez
JeromeMartinez merged commit 73aaee0 into MediaArea:main Nov 23, 2026
9 of 12 checks passed
@JeromeMartinez
JeromeMartinez deleted the Decoding_Robustness2 branch November 23, 2026 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant