Do not encode WEBP images exceeding max dimensions#2930
Merged
JimBobSquarePants merged 1 commit intomainfrom May 30, 2025
Merged
Do not encode WEBP images exceeding max dimensions#2930JimBobSquarePants merged 1 commit intomainfrom
JimBobSquarePants merged 1 commit intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
Adds a centralized dimension check for WEBP encoding and aligns boundary behavior with JPEG.
- Introduces
ThrowDimensionsTooLargeinWebpThrowHelperfor uniform errors. - Adds a guard in
WebpEncoderCoreto block overly large images. - Removes duplicate checks in
Vp8LEncoderand updates JPEG’s boundary condition.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| WebpThrowHelper.cs | New ThrowDimensionsTooLarge method for WEBP dimension errors |
| WebpEncoderCore.cs | Guard against Width/Height > MaxDimension before encoding |
| Lossless/Vp8LEncoder.cs | Removed redundant Guard.MustBeLessThan calls in WriteImageSize |
| JpegEncoderCore.cs | Switched from >= to > when comparing dimensions to max value |
Comments suppressed due to low confidence (4)
src/ImageSharp/Formats/Webp/WebpEncoderCore.cs:135
- Add unit tests to verify that images exceeding
WebpConstants.MaxDimensioncorrectly triggerImageFormatExceptionviaThrowDimensionsTooLarge.
if (image.Width > WebpConstants.MaxDimension || image.Height > WebpConstants.MaxDimension)
src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs:373
- [nitpick] Consider invoking
WebpThrowHelper.ThrowDimensionsTooLargehere if inputs exceedMaxDimension, to keep error handling consistent and avoid relying solely on upstream guards.
private void WriteImageSize(int inputImgWidth, int inputImgHeight)
src/ImageSharp/Formats/Jpeg/JpegEncoderCore.cs:61
- Update or add tests covering the changed boundary condition (
>instead of>=) to ensure the new logic matches the intended maximum dimension behavior.
if (image.Width > JpegConstants.MaxLength || image.Height > JpegConstants.MaxLength)
src/ImageSharp/Formats/Webp/WebpThrowHelper.cs:22
- Align the indentation of the new
ThrowDimensionsTooLargemethod with existing members (six spaces) to match the class’s formatting conventions.
[DoesNotReturn]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prerequisites
Description
Fixes #2920