Repository navigation
Throw PngFormatException on truncated input instead of hanging - #15
Merged
Merged
Conversation
ReadIdatChunkDataIntoStream and ReadChunkData looped until the chunk's byte count was read, but Stream.Read returns 0 at the end of the stream, so a file ending inside an IDAT or a skipped ancillary chunk never finished decoding. ReadByte turned EOF (-1) into 0xFF, and ReadBytes let EndOfStreamException escape, which isn't one of the documented exceptions. All four now throw PngFormatException. A test decodes every prefix of a valid PNG and expects that exception for each. Fixes Zeejfps#14 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 #14.
Problem
ReadIdatChunkDataIntoStreamandReadChunkDataloop until a chunk's byte count has been read, butStream.Readreturns 0 at the end of the stream, so a file that ends inside an IDAT chunk, or inside a skipped ancillary chunk, never finishes decoding (100 % CPU, no exception). Two smaller cases of the same kind:ReadByteturned EOF (-1) into0xFF, andReadBytesletEndOfStreamExceptionescape, which isn't one of the exceptions documented onPng.Decode*.Change
All four paths in
PngReadernow throwPngFormatException("Unexpected end of stream: the PNG data is truncated."). No public API changes.Tests
New
TruncatedPngTests:abCd, goes throughReadChunkData)png[..n]of a valid 16x16 RGBA PNG must throwPngFormatExceptionEach decode runs with a timeout, so a regression fails the test and doesn't hang the test run.
dotnet test -c Release, .NET 10 SDK).EndOfStreamException).🤖 Generated with Claude Code