Repository navigation
[V3] Dispose PNG chunk buffers when a chunk read throws - #3282
Merged
Merged
Conversation
…ffer-leak-on-cancel fix: Dispose chunk data buffer in ReadChunkData when read is cancelled (cherry picked from commit eefdbd9)
Both new tests cancelled at the seek after the direct IDAT data read, so both covered only the CRC read. The position restore test now cancels during the read that fills the CRC, which the position restore observes first. Also explain the ownership rule at the catch, remove an assignment that has no effect, and use BinaryPrimitives and using directives in the tests.
|
|
1 similar comment
|
|
This was referenced Nov 24, 2026
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.
Prerequisites
Description
Backport of #3203 and #3277 to 3.2.x. Fixes #3202 and #3215 on this line.
The PNG decoder allocates the chunk data before it reads the data, reads the CRC and, for
IDATandfdAT, restores the stream position. Each of these can throw, for example when the stream observes a cancellation request. The caller does not own the chunk yet, so the buffer was not disposed.ReadChunkDatadisposes the buffer when the read throws, and then rethrows.TryReadChunkdisposes the chunk data when the CRC read or the position restore throws, and then rethrows.3.2.x did not have #3203, so this PR includes it. Without it, a cancellation during the data read still leaks the buffer.
Tests:
Decode_DisposesChunkDataBuffer_WhenChunkReadIsCancelled(fix: Dispose chunk data buffer in ReadChunkData when read is cancelled #3203) covers the data read.Decode_DisposesChunkDataBuffer_WhenCancelledDuringChunkCrcReadandDecode_DisposesChunkDataBuffer_WhenCancelledDuringIdatPositionRestore(Dispose PNG chunk buffer if TryReadChunk throws after allocation #3277) cover the CRC read and the position restore.3.2.x has no
DecoderOptions(Configuration)constructor and noTestMemoryAllocator.LiveBufferBytes, so the tests use theConfigurationinitializer and check only that every allocation is returned.