Skip to content

Commit eee22e8

Browse files
Merge pull request #2931 from SixLabors/js/v3-issue-2920
V3: Do not encode WEBP images exceeding max dimensions
2 parents a7d6de9 + 5310d92 commit eee22e8

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
@@ -54,7 +54,7 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
5454
Guard.NotNull(image, nameof(image));
5555
Guard.NotNull(stream, nameof(stream));
5656

57-
if (image.Width >= JpegConstants.MaxLength || image.Height >= JpegConstants.MaxLength)
57+
if (image.Width > JpegConstants.MaxLength || image.Height > JpegConstants.MaxLength)
5858
{
5959
JpegThrowHelper.ThrowDimensionsTooLarge(image.Width, image.Height);
6060
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,6 @@ public int EncodeAlphaImageData<TPixel>(Buffer2DRegion<TPixel> frame, IMemoryOwn
374374
/// <param name="inputImgHeight">The input image height.</param>
375375
private void WriteImageSize(int inputImgWidth, int inputImgHeight)
376376
{
377-
Guard.MustBeLessThan(inputImgWidth, WebpConstants.MaxDimension, nameof(inputImgWidth));
378-
Guard.MustBeLessThan(inputImgHeight, WebpConstants.MaxDimension, nameof(inputImgHeight));
379-
380377
uint width = (uint)inputImgWidth - 1;
381378
uint height = (uint)inputImgHeight - 1;
382379

src/ImageSharp/Formats/Webp/WebpEncoderCore.cs

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

120+
if (image.Width > WebpConstants.MaxDimension || image.Height > WebpConstants.MaxDimension)
121+
{
122+
WebpThrowHelper.ThrowDimensionsTooLarge(image.Width, image.Height);
123+
}
124+
120125
bool lossless;
121126
if (this.fileFormat is not null)
122127
{

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)