Skip to content

Commit

Permalink
fix: add guards for MagickImage.Statistic (#1582)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gounlaf authored Mar 8, 2024
1 parent b5aea27 commit a338e8d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Magick.NET/MagickImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6223,7 +6223,12 @@ public void Spread(PixelInterpolateMethod method, double radius)
/// <param name="height">The height of the pixel neighborhood.</param>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public void Statistic(StatisticType type, int width, int height)
=> _nativeInstance.Statistic(type, width, height);
{
Throw.IfNegative(nameof(width), width);
Throw.IfNegative(nameof(height), height);

_nativeInstance.Statistic(type, width, height);
}

/// <summary>
/// Returns the image statistics.
Expand Down
17 changes: 17 additions & 0 deletions tests/Magick.NET.Tests/MagickImageTests/TheStatisticMethod.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,22 @@ public partial class MagickImageTests
{
public class TheStatisticMethod
{
[Fact]
public void ShouldThrowExceptionWhenWidthIsNegative()
{
using var image = new MagickImage(Files.NoisePNG);

Assert.Throws<ArgumentException>("width", () => image.Statistic(StatisticType.Minimum, -1, 1));
}

[Fact]
public void ShouldThrowExceptionWhenHeightIsNegative()
{
using var image = new MagickImage(Files.NoisePNG);

Assert.Throws<ArgumentException>("height", () => image.Statistic(StatisticType.Minimum, 10, -1));
}

[Fact]
public void ShouldChangePixels()
{
Expand Down

0 comments on commit a338e8d

Please sign in to comment.