Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ private void ProcessHeader(BufferedReadStream stream)
ThrowPrematureEof();
}

if (this.maxPixelValue <= 0 || this.maxPixelValue >= 65536)
{
throw new InvalidImageContentException("Invalid max pixel value.");
}

if (this.maxPixelValue > 255)
{
this.componentType = PbmComponentType.Short;
Expand Down
13 changes: 13 additions & 0 deletions tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ public void PbmDecoder_Decode_Resize<TPixel>(TestImageProvider<TPixel> provider)
appendPixelTypeToFileName: false);
}

[Theory]
[InlineData("P2")]
[InlineData("P3")]
[InlineData("P5")]
[InlineData("P6")]
public void Decode_MaxPixelValueZero_ThrowsInvalidImageContentException(string magic)
{
byte[] bytes = Encoding.ASCII.GetBytes($"{magic} 1 1 0\n0");
using MemoryStream stream = new(bytes);

Assert.Throws<InvalidImageContentException>(() => Image.Load<Rgba32>(stream));
}

[Fact]
public void PlainText_PrematureEof()
{
Expand Down
Loading