Skip to content

Commit a804602

Browse files
Do not encode WEBP images exceeding max dimensions
1 parent 4b29744 commit a804602

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
5858
Guard.NotNull(image, nameof(image));
5959
Guard.NotNull(stream, nameof(stream));
6060

61-
if (image.Width >= JpegConstants.MaxLength || image.Height >= JpegConstants.MaxLength)
61+
if (image.Width > JpegConstants.MaxLength || image.Height > JpegConstants.MaxLength)
6262
{
6363
JpegThrowHelper.ThrowDimensionsTooLarge(image.Width, image.Height);
6464
}

src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,6 @@ public int EncodeAlphaImageData<TPixel>(Buffer2DRegion<TPixel> frame, IMemoryOwn
371371
/// <param name="inputImgHeight">The input image height.</param>
372372
private void WriteImageSize(int inputImgWidth, int inputImgHeight)
373373
{
374-
Guard.MustBeLessThan(inputImgWidth, WebpConstants.MaxDimension, nameof(inputImgWidth));
375-
Guard.MustBeLessThan(inputImgHeight, WebpConstants.MaxDimension, nameof(inputImgHeight));
376-
377374
uint width = (uint)inputImgWidth - 1;
378375
uint height = (uint)inputImgHeight - 1;
379376

src/ImageSharp/Formats/Webp/WebpEncoderCore.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
132132
Guard.NotNull(image, nameof(image));
133133
Guard.NotNull(stream, nameof(stream));
134134

135+
if (image.Width > WebpConstants.MaxDimension || image.Height > WebpConstants.MaxDimension)
136+
{
137+
WebpThrowHelper.ThrowDimensionsTooLarge(image.Width, image.Height);
138+
}
139+
135140
bool lossless;
136141
if (this.fileFormat is not null)
137142
{

src/ImageSharp/Formats/Webp/WebpThrowHelper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ internal static class WebpThrowHelper
1818

1919
[DoesNotReturn]
2020
public static void ThrowInvalidImageDimensions(string errorMessage) => throw new InvalidImageContentException(errorMessage);
21+
22+
[DoesNotReturn]
23+
public static void ThrowDimensionsTooLarge(int width, int height) => throw new ImageFormatException($"Image is too large to encode at {width}x{height} for WEBP format.");
2124
}

0 commit comments

Comments
 (0)