Repository navigation
Dispose PNG chunk buffer if TryReadChunk throws after allocation - #3277
Merged
JimBobSquarePants merged 3 commits intoNov 23, 2026
Merged
JimBobSquarePants merged 3 commits into
JimBobSquarePants merged 3 commits into
Conversation
|
|
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.
This was referenced Nov 23, 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.
Fixes #3215
Root Cause
In
PngDecoderCore.TryReadChunk, onceReadChunkDataallocateschunk.Data(anIMemoryOwner<byte>), the caller does not yet ownchunkifTryReadChunkthrows before returning. WhileValidateChunkdisposedchunk.DataonException, any exception (includingOperationCanceledException) thrown afterValidateChunk— such as restoringthis.currentStream.PositionforIDAT/fdATchunks — or ifValidateChunkis refactored would leak the pooledchunk.Databuffer until GC finalization.Fix
ValidateChunkand theIDAT/fdATstream position restoration inTryReadChunkwith atry/catchthat disposeschunk.Dataand resetschunk = defaultbefore rethrowing.try/catchdisposal insideValidateChunksinceTryReadChunkis its only caller.PngDecoderTests.Cancellation.csusingTestMemoryAllocatorto verify all allocated buffers are disposed when cancellation occurs during chunk CRC reading or IDAT position restoration.Verification
dotnet test --no-restore tests/ImageSharp.Tests/ImageSharp.Tests.csproj --filter FullyQualifiedName~SixLabors.ImageSharp.Tests.Formats.Png.PngDecoderTests: 237 passed, 0 failed, 0 skipped (net10.0).