Validate decoded image dimensions · SixLabors/ImageSharp@7781fbd · GitHub
Skip to content

Commit 7781fbd

Browse files
Validate decoded image dimensions
1 parent 0815358 commit 7781fbd

4 files changed

Lines changed: 29 additions & 7 deletions

File tree

‎src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs‎

Lines changed: 5 additions & 0 deletions

‎src/ImageSharp/Formats/Gif/GifDecoderCore.cs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,6 @@ protected override ImageInfo Identify(BufferedReadStream stream, CancellationTok
261261
this.currentLocalColorTable?.Dispose();
262262
}
263263

264-
if (this.logicalScreenDescriptor.Width == 0 && this.logicalScreenDescriptor.Height == 0)
265-
{
266-
GifThrowHelper.ThrowNoHeader();
267-
}
268-
269264
// Ignoring a malformed ancillary extension must not let identify succeed for a file
270265
// that never contained any readable image frame data.
271266
if (previousFrame is null)
@@ -328,6 +323,10 @@ private void ReadLogicalScreenDescriptor(BufferedReadStream stream)
328323
}
329324

330325
this.logicalScreenDescriptor = GifLogicalScreenDescriptor.Parse(this.buffer);
326+
if (this.logicalScreenDescriptor.Width == 0 || this.logicalScreenDescriptor.Height == 0)
327+
{
328+
GifThrowHelper.ThrowInvalidImageContentException("Width and height must be greater than 0.");
329+
}
331330
}
332331

333332
/// <summary>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ protected override Image<TPixel> Decode<TPixel>(BufferedReadStream stream, Cance
7272
TgaThrowHelper.ThrowNotSupportedException($"Unknown tga colormap type {this.fileHeader.ColorMapType} found");
7373
}
7474

75-
if (this.fileHeader.Width == 0 || this.fileHeader.Height == 0)
75+
if (this.fileHeader.Width <= 0 || this.fileHeader.Height <= 0)
7676
{
77-
throw new UnknownImageFormatException("Width or height cannot be 0");
77+
TgaThrowHelper.ThrowInvalidImageContentException("Width and height must be greater than 0.");
7878
}
7979

8080
Image<TPixel> image = new(this.configuration, this.fileHeader.Width, this.fileHeader.Height, this.metadata);
Lines changed: 18 additions & 0 deletions

0 commit comments

Comments
 (0)