Repository navigation
Decoding robustness: no Cluster, FLAC block size, buffered read - #559
Merged
JeromeMartinez merged 7 commits intoNov 23, 2026
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up of #558, fixes found during its review.
--checkOK 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.-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_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 unusedCopyCut()is removed.--ioother thanmmap): 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 tomunmap(),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.