-
-
Notifications
You must be signed in to change notification settings - Fork 852
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
PBM decoder robustness improvements and BufferedReadStream observability #2551
Changes from 4 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() || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like this! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only thing I would suggest is to change the method names to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've pushed those changes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ha, didn't realize you'd explicitly removed them! I can revert at a later time if you feel strongly about it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really prefer it to be the other way. By convention, I like following such conventions to the letter, because ambiguity can lead to a misunderstandings and programming mistakes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m not precious about it (and happy to revert) but there’s something a little clunky about the method there. We’re really returning two things, the decimal and the stream state. I’ve seen something in the runtime I’m sure that handles it better (Maybe inside guid parsing code) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I considered returning a |
||
!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
rowSpan
with 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 (
BuffferedReadStream
is hitting EOF twice &FromL8Bytes
is 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