Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 0 additions & 2 deletions src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1471,8 +1471,6 @@ private int ReadImageHeaders(BufferedReadStream stream, out bool inverted, out b
}
}

this.infoHeader.VerifyDimensions();

int skipAmount = this.fileHeader.Offset - (int)this.stream.Position;
if ((skipAmount + (int)this.stream.Position) > this.stream.Length)
{
Expand Down
22 changes: 10 additions & 12 deletions src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ internal struct BmpInfoHeader
/// </summary>
public const int HeaderSizeSize = 4;

/// <summary>
/// Maximum dimensions of a bitmap with or height: 2**31 - 1, since width and height are int32
/// </summary>
private const int MaximumBmpDimension = 2147483647;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not being used?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it's no longer used, it's now removed.


/// <summary>
/// Maximum size of a bitmap width * height: 2**32, since size is uint32.
/// </summary>
private const long MaximumBmpSize = 4294967296;

public BmpInfoHeader(
int headerSize,
int width,
Expand Down Expand Up @@ -542,17 +552,5 @@ public void WriteV5Header(Span<byte> buffer)

dest = this;
}

internal void VerifyDimensions()
{
const int MaximumBmpDimension = 65535;

if (this.Width > MaximumBmpDimension || this.Height > MaximumBmpDimension)
{
throw new InvalidOperationException(
$"The input bmp '{this.Width}x{this.Height}' is "
+ $"bigger then the max allowed size '{MaximumBmpDimension}x{MaximumBmpDimension}'");
}
}
}
}
16 changes: 16 additions & 0 deletions tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs
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;
using System.IO;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
Expand Down Expand Up @@ -350,6 +351,21 @@ public void Encode_PreservesColorProfile<TPixel>(TestImageProvider<TPixel> provi
Assert.Equal(expectedProfileBytes, actualProfileBytes);
}

[Theory]
[InlineData(1, 66535)]
[InlineData(66535, 1)]
public void Encode_WorksWithSizeGreaterThen65k(int width, int height)
{
Exception exception = Record.Exception(() =>
{
using Image image = new Image<Rgba32>(width, height);
using var memStream = new MemoryStream();
image.Save(memStream, BmpEncoder);
});

Assert.Null(exception);
}

[Theory]
[WithFile(Car, PixelTypes.Rgba32, BmpBitsPerPixel.Pixel32)]
[WithFile(V5Header, PixelTypes.Rgba32, BmpBitsPerPixel.Pixel32)]
Expand Down