Browse Source

Merge pull request #2930 from SixLabors/js/issue-2920

Do not encode WEBP images exceeding max dimensions
pull/2936/head
James Jackson-South 8 months ago
committed by GitHub
parent
commit
1226eb541c
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs
  2. 3
      src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs
  3. 5
      src/ImageSharp/Formats/Webp/WebpEncoderCore.cs
  4. 3
      src/ImageSharp/Formats/Webp/WebpThrowHelper.cs

2
src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs

@ -58,7 +58,7 @@ internal sealed unsafe partial class JpegEncoderCore
Guard.NotNull(image, nameof(image));
Guard.NotNull(stream, nameof(stream));
if (image.Width >= JpegConstants.MaxLength || image.Height >= JpegConstants.MaxLength)
if (image.Width > JpegConstants.MaxLength || image.Height > JpegConstants.MaxLength)
{
JpegThrowHelper.ThrowDimensionsTooLarge(image.Width, image.Height);
}

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

@ -371,9 +371,6 @@ internal class Vp8LEncoder : IDisposable
/// <param name="inputImgHeight">The input image height.</param>
private void WriteImageSize(int inputImgWidth, int inputImgHeight)
{
Guard.MustBeLessThan(inputImgWidth, WebpConstants.MaxDimension, nameof(inputImgWidth));
Guard.MustBeLessThan(inputImgHeight, WebpConstants.MaxDimension, nameof(inputImgHeight));
uint width = (uint)inputImgWidth - 1;
uint height = (uint)inputImgHeight - 1;

5
src/ImageSharp/Formats/Webp/WebpEncoderCore.cs

@ -132,6 +132,11 @@ internal sealed class WebpEncoderCore
Guard.NotNull(image, nameof(image));
Guard.NotNull(stream, nameof(stream));
if (image.Width > WebpConstants.MaxDimension || image.Height > WebpConstants.MaxDimension)
{
WebpThrowHelper.ThrowDimensionsTooLarge(image.Width, image.Height);
}
bool lossless;
if (this.fileFormat is not null)
{

3
src/ImageSharp/Formats/Webp/WebpThrowHelper.cs

@ -18,4 +18,7 @@ internal static class WebpThrowHelper
[DoesNotReturn]
public static void ThrowInvalidImageDimensions(string errorMessage) => throw new InvalidImageContentException(errorMessage);
[DoesNotReturn]
public static void ThrowDimensionsTooLarge(int width, int height) => throw new ImageFormatException($"Image is too large to encode at {width}x{height} for WEBP format.");
}

Loading…
Cancel
Save