Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add guards for MagickImage.Shave #1581

Merged
merged 2 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Magick.NET/MagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6012,7 +6012,12 @@ public void Shave(int size)
/// <param name="topBottom">The number of pixels to shave top and bottom.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Shave(int leftRight, int topBottom)
=> _nativeInstance.Shave(leftRight, topBottom);
{
Throw.IfNegative(nameof(leftRight), leftRight);
Throw.IfNegative(nameof(topBottom), topBottom);

_nativeInstance.Shave(leftRight, topBottom);
}

/// <summary>
/// Shear image (create parallelogram by sliding image by X or Y axis).
Expand Down
25 changes: 25 additions & 0 deletions tests/Magick.NET.Tests/MagickImageTests/TheShaveMethod.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/Magick.NET.
// Licensed under the Apache License, Version 2.0.

using System;
using ImageMagick;
using Xunit;

Expand All @@ -10,6 +11,30 @@ public partial class MagickImageTests
{
public class TheShaveMethod
{
[Fact]
public void ShouldThrowExceptionWhenSizeIsNegative()
{
using var image = new MagickImage();

Assert.Throws<ArgumentException>("leftRight", () => image.Shave(-1));
}

[Fact]
public void ShouldThrowExceptionWhenLeftRightIsNegative()
{
using var image = new MagickImage();

Assert.Throws<ArgumentException>("leftRight", () => image.Shave(-1, 40));
}

[Fact]
public void ShouldThrowExceptionWhenTopBottomIsNegative()
{
using var image = new MagickImage();

Assert.Throws<ArgumentException>("topBottom", () => image.Shave(20, -1));
}

[Fact]
public void ShouldShaveSizeFromEdges()
{
Expand Down
Loading