Skip to content

Commit 8e6cab9

Browse files
Validate PBM max pixel value
1 parent 8f2716b commit 8e6cab9

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ private void ProcessHeader(BufferedReadStream stream)
155155
ThrowPrematureEof();
156156
}
157157

158+
if (this.maxPixelValue <= 0 || this.maxPixelValue >= 65536)
159+
{
160+
throw new InvalidImageContentException("Invalid max pixel value.");
161+
}
162+
158163
if (this.maxPixelValue > 255)
159164
{
160165
this.componentType = PbmComponentType.Short;

tests/ImageSharp.Tests/Formats/Pbm/PbmDecoderTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ public void PbmDecoder_Decode_Resize<TPixel>(TestImageProvider<TPixel> provider)
123123
appendPixelTypeToFileName: false);
124124
}
125125

126+
[Theory]
127+
[InlineData("P2")]
128+
[InlineData("P3")]
129+
[InlineData("P5")]
130+
[InlineData("P6")]
131+
public void Decode_MaxPixelValueZero_ThrowsInvalidImageContentException(string magic)
132+
{
133+
byte[] bytes = Encoding.ASCII.GetBytes($"{magic} 1 1 0\n0");
134+
using MemoryStream stream = new(bytes);
135+
136+
Assert.Throws<InvalidImageContentException>(() => Image.Load<Rgba32>(stream));
137+
}
138+
126139
[Fact]
127140
public void PlainText_PrematureEof()
128141
{

0 commit comments

Comments
 (0)