diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index e1850a32b..2d7014335 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/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(this.configuration, this.fileHeader.Width, this.fileHeader.Height, this.metadata); Buffer2D pixels = image.GetRootFramePixelBuffer(); diff --git a/src/ImageSharp/Formats/Tga/TgaImageFormatDetector.cs b/src/ImageSharp/Formats/Tga/TgaImageFormatDetector.cs index 5a0b0f44c..bd9cfa900 100644 --- a/src/ImageSharp/Formats/Tga/TgaImageFormatDetector.cs +++ b/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(); } diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpFileHeaderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpFileHeaderTests.cs index 140686708..4c3fe3149 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpFileHeaderTests.cs +++ b/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(() => - { - using (Image.Load(Configuration.Default, this.Stream, out IImageFormat _)) - { - } - }); - } } } diff --git a/tests/ImageSharp.Tests/Formats/Tga/TgaFileHeaderTests.cs b/tests/ImageSharp.Tests/Formats/Tga/TgaFileHeaderTests.cs index 943144d61..c227b7957 100644 --- a/tests/ImageSharp.Tests/Formats/Tga/TgaFileHeaderTests.cs +++ b/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(() => {