Skip to content

Commit ba5d39d

Browse files
Fix #3067
1 parent 4224257 commit ba5d39d

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ protected override Image<TPixel> Decode<TPixel>(BufferedReadStream stream, Cance
126126
switch (this.infoHeader.Compression)
127127
{
128128
case BmpCompression.RGB:
129-
if (this.infoHeader.BitsPerPixel == 32)
129+
130+
ushort bitsPerPixel = this.infoHeader.BitsPerPixel;
131+
132+
if (bitsPerPixel == 32)
130133
{
131134
if (this.bmpMetadata.InfoHeaderType == BmpInfoHeaderType.WinVersion3)
132135
{
@@ -137,26 +140,30 @@ protected override Image<TPixel> Decode<TPixel>(BufferedReadStream stream, Cance
137140
this.ReadRgb32Fast(stream, pixels, this.infoHeader.Width, this.infoHeader.Height, inverted);
138141
}
139142
}
140-
else if (this.infoHeader.BitsPerPixel == 24)
143+
else if (bitsPerPixel == 24)
141144
{
142145
this.ReadRgb24(stream, pixels, this.infoHeader.Width, this.infoHeader.Height, inverted);
143146
}
144-
else if (this.infoHeader.BitsPerPixel == 16)
147+
else if (bitsPerPixel == 16)
145148
{
146149
this.ReadRgb16(stream, pixels, this.infoHeader.Width, this.infoHeader.Height, inverted);
147150
}
148-
else if (this.infoHeader.BitsPerPixel <= 8)
151+
else if (bitsPerPixel is > 0 and <= 8)
149152
{
150153
this.ReadRgbPalette(
151154
stream,
152155
pixels,
153156
palette,
154157
this.infoHeader.Width,
155158
this.infoHeader.Height,
156-
this.infoHeader.BitsPerPixel,
159+
bitsPerPixel,
157160
bytesPerColorMapEntry,
158161
inverted);
159162
}
163+
else
164+
{
165+
BmpThrowHelper.ThrowInvalidImageContentException($"Invalid bits per pixel: {bitsPerPixel}");
166+
}
160167

161168
break;
162169

tests/ImageSharp.Tests/Formats/Bmp/BmpDecoderTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,4 +570,27 @@ public void BmpDecoder_ThrowsException_Issue2696<TPixel>(TestImageProvider<TPixe
570570
});
571571
Assert.IsType<InvalidMemoryOperationException>(ex.InnerException);
572572
}
573+
574+
[Fact]
575+
public void BmpDecoder_ThrowsException_Issue3067()
576+
{
577+
// Construct minimal BMP with bitsPerPixel = 0
578+
byte[] bmp = new byte[54];
579+
bmp[0] = (byte)'B';
580+
bmp[1] = (byte)'M';
581+
BitConverter.GetBytes(54).CopyTo(bmp, 2);
582+
BitConverter.GetBytes(54).CopyTo(bmp, 10);
583+
BitConverter.GetBytes(40).CopyTo(bmp, 14);
584+
BitConverter.GetBytes(1).CopyTo(bmp, 18);
585+
BitConverter.GetBytes(1).CopyTo(bmp, 22);
586+
BitConverter.GetBytes((short)1).CopyTo(bmp, 26);
587+
BitConverter.GetBytes((short)0).CopyTo(bmp, 28); // bitsPerPixel = 0
588+
589+
using MemoryStream stream = new(bmp);
590+
591+
InvalidImageContentException ex = Assert.Throws<InvalidImageContentException>(() =>
592+
{
593+
using Image image = BmpDecoder.Instance.Decode(DecoderOptions.Default, stream);
594+
});
595+
}
573596
}

0 commit comments

Comments
 (0)