Skip to content

Commit 68ccc1d

Browse files
Merge branch 'main' into dependabot/github_actions/actions/checkout-6
2 parents 6bb3624 + 461c021 commit 68ccc1d

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,12 @@ private int ReadImageHeaders(BufferedReadStream stream, out bool inverted, out b
15481548
case BmpFileMarkerType.Bitmap:
15491549
if (this.fileHeader.HasValue)
15501550
{
1551+
if (this.fileHeader.Value.Offset > stream.Length)
1552+
{
1553+
BmpThrowHelper.ThrowInvalidImageContentException(
1554+
$"Pixel data offset {this.fileHeader.Value.Offset} exceeds file size {stream.Length}.");
1555+
}
1556+
15511557
colorMapSizeBytes = this.fileHeader.Value.Offset - BmpFileHeader.Size - this.infoHeader.HeaderSize;
15521558
}
15531559
else

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,7 @@ public void BmpDecoder_ThrowsException_Issue2696<TPixel>(TestImageProvider<TPixe
572572
Assert.IsType<InvalidMemoryOperationException>(ex.InnerException);
573573
}
574574

575+
// https://github.com/SixLabors/ImageSharp/issues/3067
575576
[Fact]
576577
public void BmpDecoder_ThrowsException_Issue3067()
577578
{
@@ -594,4 +595,32 @@ public void BmpDecoder_ThrowsException_Issue3067()
594595
using Image image = BmpDecoder.Instance.Decode(DecoderOptions.Default, stream);
595596
});
596597
}
598+
599+
// https://github.com/SixLabors/ImageSharp/issues/3074
600+
[Fact]
601+
public void BmpDecoder_ThrowsException_Issue3074()
602+
{
603+
// Crafted BMP: pixel data offset = 0x7FFFFFFF, actual file = 35 bytes
604+
byte[] data =
605+
[
606+
0x42, 0x4D, // "BM" signature
607+
0x3A, 0x00, 0x00, 0x00, // file size: 58
608+
0x00, 0x00, 0x00, 0x00, // reserved
609+
0xFF, 0xFF, 0xFF, 0x7F, // pixel offset: 0x7FFFFFFF (2,147,483,647)
610+
0x28, 0x00, 0x00, 0x00, // DIB header size: 40
611+
0x01, 0x00, 0x00, 0x00, // width: 1
612+
0x01, 0xFF, 0x00, 0x00, // height: 65281
613+
0x01, 0x00, // color planes: 1
614+
0x08, 0x00, // bits per pixel: 8
615+
0x00, 0x00, 0x00, 0x00, // compression: RGB
616+
0x00, 0x00, 0x00 // (truncated)
617+
];
618+
619+
using MemoryStream stream = new(data);
620+
621+
Assert.Throws<InvalidImageContentException>(() =>
622+
{
623+
using Image<Rgba32> image = Image.Load<Rgba32>(stream);
624+
});
625+
}
597626
}

0 commit comments

Comments
 (0)