From 04eef159b3a4a52b99adf935550ee6942cce9e85 Mon Sep 17 00:00:00 2001 From: Dmitry Pentin Date: Fri, 16 Jul 2021 02:07:32 +0300 Subject: [PATCH] Added frame dimensions proper check & comments --- .../Formats/Jpeg/JpegDecoderCore.cs | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 3c262be329..45d08dadf5 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -835,21 +835,29 @@ namespace SixLabors.ImageSharp.Formats.Jpeg JpegThrowHelper.ThrowInvalidImageContentException("Only 8-Bit and 12-Bit precision supported."); } + // 2 byte: height + int frameHeight = (this.temp[1] << 8) | this.temp[2]; + + // 2 byte: width + int frameWidth = (this.temp[3] << 8) | this.temp[4]; + + // Validity check: width/height > 0 (they are upper-bounded by 2 byte max value so no need to check that) + if (frameHeight == 0 || frameWidth == 0) + { + JpegThrowHelper.ThrowInvalidImageDimensions(this.Frame.PixelWidth, this.Frame.PixelHeight); + } + + this.Frame = new JpegFrame { Extended = frameMarker.Marker == JpegConstants.Markers.SOF1, Progressive = frameMarker.Marker == JpegConstants.Markers.SOF2, Precision = precision, - PixelHeight = (this.temp[1] << 8) | this.temp[2], - PixelWidth = (this.temp[3] << 8) | this.temp[4], + PixelHeight = frameHeight, + PixelWidth = frameWidth, ComponentCount = this.temp[5] }; - if (this.Frame.PixelWidth == 0 || this.Frame.PixelHeight == 0) - { - JpegThrowHelper.ThrowInvalidImageDimensions(this.Frame.PixelWidth, this.Frame.PixelHeight); - } - this.ImageSizeInPixels = new Size(this.Frame.PixelWidth, this.Frame.PixelHeight); this.ComponentCount = this.Frame.ComponentCount;