-
-
Notifications
You must be signed in to change notification settings - Fork 888
PBM decoder robustness improvements and BufferedReadStream observability #2551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
8241a5a
fecaa53
a1d7284
c74fbec
c359d13
e5ba24c
9b42de6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Copyright (c) Six Labors. | ||
| // Licensed under the Six Labors Split License. | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
| using SixLabors.ImageSharp.IO; | ||
| using SixLabors.ImageSharp.Memory; | ||
| using SixLabors.ImageSharp.Metadata; | ||
|
|
@@ -144,14 +145,22 @@ private void ProcessHeader(BufferedReadStream stream) | |
| throw new InvalidImageContentException("Unknown of not implemented image type encountered."); | ||
| } | ||
|
|
||
| stream.SkipWhitespaceAndComments(); | ||
| int width = stream.ReadDecimal(); | ||
| stream.SkipWhitespaceAndComments(); | ||
| int height = stream.ReadDecimal(); | ||
| stream.SkipWhitespaceAndComments(); | ||
| if (!stream.SkipWhitespaceAndComments() || | ||
|
||
| !stream.ReadDecimal(out int width) || | ||
| !stream.SkipWhitespaceAndComments() || | ||
| !stream.ReadDecimal(out int height) || | ||
| !stream.SkipWhitespaceAndComments()) | ||
| { | ||
| ThrowPrematureEof(); | ||
| } | ||
|
|
||
| if (this.colorType != PbmColorType.BlackAndWhite) | ||
| { | ||
| this.maxPixelValue = stream.ReadDecimal(); | ||
| if (!stream.ReadDecimal(out this.maxPixelValue)) | ||
| { | ||
| ThrowPrematureEof(); | ||
| } | ||
|
|
||
| if (this.maxPixelValue > 255) | ||
| { | ||
| this.componentType = PbmComponentType.Short; | ||
|
|
@@ -174,6 +183,9 @@ private void ProcessHeader(BufferedReadStream stream) | |
| meta.Encoding = this.encoding; | ||
| meta.ColorType = this.colorType; | ||
| meta.ComponentType = this.componentType; | ||
|
|
||
| [DoesNotReturn] | ||
| static void ThrowPrematureEof() => throw new InvalidImageContentException("Reached EOF while reading the header."); | ||
| } | ||
|
|
||
| private void ProcessPixels<TPixel>(BufferedReadStream stream, Buffer2D<TPixel> pixels) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is lame actually. I should have sliced down
rowSpanwith the result ofstream.Read(), for the pixel conversion done later, or used the conditionstream.Read(rowSpan) < rowSpan.Length(the latter isstricter, but simpler).This has little practical relevance (
BuffferedReadStreamis hitting EOF twice &FromL8Bytesis converting some memory garbage for broken files), so no need to fix it now, but the code looks stupid :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can improve it with follow up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#2552