Browse Source

Throw ImageFormatException when width or height is 0

af/merge-core
Brian Popow 6 years ago
parent
commit
7db0caaedd
  1. 5
      src/ImageSharp/Formats/Tga/TgaDecoderCore.cs
  2. 8
      src/ImageSharp/Formats/Tga/TgaImageFormatDetector.cs
  3. 16
      tests/ImageSharp.Tests/Formats/Bmp/BmpFileHeaderTests.cs
  4. 8
      tests/ImageSharp.Tests/Formats/Tga/TgaFileHeaderTests.cs

5
src/ImageSharp/Formats/Tga/TgaDecoderCore.cs

@ -85,6 +85,11 @@ namespace SixLabors.ImageSharp.Formats.Tga
TgaThrowHelper.ThrowNotSupportedException($"Unknown tga colormap type {this.fileHeader.ColorMapType} found");
}
if (this.fileHeader.Width == 0 || this.fileHeader.Height == 0)
{
throw new UnknownImageFormatException("Width or height cannot be 0");
}
var image = new Image<TPixel>(this.configuration, this.fileHeader.Width, this.fileHeader.Height, this.metadata);
Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer();

8
src/ImageSharp/Formats/Tga/TgaImageFormatDetector.cs

@ -23,7 +23,13 @@ namespace SixLabors.ImageSharp.Formats.Tga
{
if (header.Length >= this.HeaderSize)
{
// There is no magick bytes in a tga file, so at least the image type in the header will be checked for a valid value.
// There is no magick bytes in a tga file, so at least the image type
// and the colormap type in the header will be checked for a valid value.
if (header[1] != 0 && header[1] != 1)
{
return false;
}
var imageType = (TgaImageType)header[2];
return imageType.IsValid();
}

16
tests/ImageSharp.Tests/Formats/Bmp/BmpFileHeaderTests.cs

@ -3,6 +3,7 @@
using System;
using System.IO;
using System.Linq;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
@ -12,10 +13,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
{
public class BmpFileHeaderTests
{
private static readonly byte[] Data = BitConverter.GetBytes(BmpConstants.TypeMarkers.Bitmap);
private MemoryStream Stream { get; } = new MemoryStream(Data);
[Fact]
public void TestWrite()
{
@ -27,16 +24,5 @@ namespace SixLabors.ImageSharp.Tests.Formats.Bmp
Assert.Equal("AQACAAAAAwAAAAQAAAA=", Convert.ToBase64String(buffer));
}
[Fact]
public void ImageLoad_WithoutEnoughData_Throws_UnknownImageFormatException()
{
Assert.Throws<UnknownImageFormatException>(() =>
{
using (Image.Load(Configuration.Default, this.Stream, out IImageFormat _))
{
}
});
}
}
}

8
tests/ImageSharp.Tests/Formats/Tga/TgaFileHeaderTests.cs

@ -11,12 +11,16 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tga
{
public class TgaFileHeaderTests
{
private static readonly byte[] Data = { 0, 0, 0 };
private static readonly byte[] Data = {
0,
0,
15 // invalid tga image type
};
private MemoryStream Stream { get; } = new MemoryStream(Data);
[Fact]
public void ImageLoad_WithoutEnoughData_Throws_UnknownImageFormatException()
public void ImageLoad_WithInvalidImageType_Throws_UnknownImageFormatException()
{
Assert.Throws<UnknownImageFormatException>(() =>
{

Loading…
Cancel
Save