Reject a negative padding and a non-positive size - #118
Merged
Merged
Conversation
Validate only checked padding's upper bound, which is not a bound at all for a negative number: -5 >= 64 is false. EffectivePadding then handed the value back unchanged, the content was resized to finalSize - padding * 2 -- larger than the canvas -- and ImageSharp's Pad never shrinks an image, so the output PNG came out bigger than the documented min(squareSize, size), quietly and with exit code 0. Size gets its own check for the same reason. It was only ever caught incidentally by the padding comparison derived from it, which stops being a net once padding is negative too: -20 >= -10 / 2 is false, so a negative size and a negative padding together passed validation untouched. A negative padding is reported on its own rather than alongside "less than half the size of the image", which is not the complaint about a negative number, and half of an invalid size is not a bound worth quoting. Fixes #115 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012betVHk3gFcj5RYkEe4vrm
|
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.



Fixes #115
The defect
Arguments.Validate()checked only padding's upper bound:Which is not a bound at all for a negative number —
-5 >= 64is false.EffectivePaddingthen hands the value back unchanged, the content is resized tofinalSize - padding * 2(larger than the canvas), and ImageSharp'sPadnever shrinks an image. The output PNG comes out bigger than the documentedmin(squareSize, size), quietly, with exit code 0.That breaks the invariant
README.mdandCLAUDE.mdboth state outright: padding insets the content, it never changes the canvas.The change
Two checks, and a small restructure so each complaint is the right one:
Sizegets its own check, which the issue raises as worth double-checking and which turns out to matter. It was only ever caught incidentally, by the padding comparison derived from it — and that stops being a net the moment padding is negative too:-20 >= -10 / 2is false, so--size -10 --padding -20passed validation completely untouched. Where it did catch something, it lied about it:--size 0reported "Padding must be less than half the size of the image."The two padding messages are exclusive. "Less than half the size of the image" is not the complaint about a negative number, and half of an invalid size is not a bound worth quoting.
ValidateReportsEveryProblemAtOncestill reports every distinct problem at once; this only stops one problem being reported twice in two voices.Rejected, not clamped.
EffectivePaddingalready clamps padding downward against the effective canvas, so clamping a negative value up to 0 would have been the locally consistent move. It is the wrong one: the downward clamp exists becauseValidatecannot knowfinalSizeyet, which is a genuine limit of where the check sits. A negative padding needs no such knowledge — it is unusable at any canvas size — so it belongs at the gate, where the caller finds out rather than silently getting something else.Tests
ValidateRejectsNegativePadding--size 128 --padding -5ValidateRejectsASizeThatIsNotPositive--size 0, and that it now says so rather than blaming paddingValidateRejectsANegativeSizeAndPaddingTogetherProved failing without the fix. Reverting only
IconHelper/Arguments.csand keeping the tests takes the suite from 8 failures to 11 — the three new ones, nothing else moving:The existing padding tests —
ValidateAcceptsPaddingBelowHalfTheSize,ValidateRejectsPaddingEqualToHalfTheSize,ValidateUsesIntegerDivisionForOddSizes,ValidateReportsEveryProblemAtOnce— pass unchanged, so the upper bound and its exact message are untouched.Verification
dotnet build IconHelper.sln -c Release— succeeded, 0 warnings, 0 errors (analyzers run as errors here)dotnet test IconHelper.sln -c Release— 76 total, 68 passed, 8 failedThe 8 failures are
GoldMasterTestsand are not from this change: unmodifiedmaingives 73 total, 65 passed, the same 8 failed. The gold master fixtures underIconHelper.Test/GoldMaster/Input/are Git LFS objects,git-lfsis not installed in this container, and a shallow clone leaves them as pointer files that ImageSharp cannot decode. CI, which materializes LFS content, should see all 76 green. The same note is on #117.Docs
README.md's padding section gains two sentences stating that a negative--paddingis rejected rather than clamped, and why. It sits directly under the "the output is alwaysfinalSizesquare whatever the padding" claim, which this makes true rather than aspirational.Relationship to #117
Independent, and branched separately from
main— #117 is the.new.pngpath-versus-name fix inIconHelper.cs, this is validation inArguments.cs. No shared files, so they can merge in either order.🤖 Generated with Claude Code
https://claude.ai/code/session_012betVHk3gFcj5RYkEe4vrm
Generated by Claude Code