From 1151c98f935e7a6784a1ed957b30d5e180ceaed4 Mon Sep 17 00:00:00 2001 From: Florian Levis Date: Fri, 8 Mar 2024 18:19:24 +0100 Subject: [PATCH] fix: add guards for MagickImage.Statistic --- src/Magick.NET/MagickImage.cs | 7 ++++++- .../MagickImageTests/TheStatisticMethod.cs | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Magick.NET/MagickImage.cs b/src/Magick.NET/MagickImage.cs index e873129de6..1104af7b7b 100644 --- a/src/Magick.NET/MagickImage.cs +++ b/src/Magick.NET/MagickImage.cs @@ -6252,7 +6252,12 @@ public void Spread(PixelInterpolateMethod method, double radius) /// The height of the pixel neighborhood. /// Thrown when an error is raised by ImageMagick. 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); + } /// /// Returns the image statistics. diff --git a/tests/Magick.NET.Tests/MagickImageTests/TheStatisticMethod.cs b/tests/Magick.NET.Tests/MagickImageTests/TheStatisticMethod.cs index f034f637c9..7ef3011552 100644 --- a/tests/Magick.NET.Tests/MagickImageTests/TheStatisticMethod.cs +++ b/tests/Magick.NET.Tests/MagickImageTests/TheStatisticMethod.cs @@ -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; @@ -10,6 +11,22 @@ public partial class MagickImageTests { public class TheStatisticMethod { + [Fact] + public void ShouldThrowExceptionWhenWidthIsNegative() + { + using var image = new MagickImage(Files.NoisePNG); + + Assert.Throws("width", () => image.Statistic(StatisticType.Minimum, -1, 1)); + } + + [Fact] + public void ShouldThrowExceptionWhenHeightIsNegative() + { + using var image = new MagickImage(Files.NoisePNG); + + Assert.Throws("height", () => image.Statistic(StatisticType.Minimum, 10, -1)); + } + [Fact] public void ShouldChangePixels() {