From a338e8d60f0f9baf2b802beb8e896c88b51d51a6 Mon Sep 17 00:00:00 2001 From: Florian Levis Date: Fri, 8 Mar 2024 20:09:18 +0100 Subject: [PATCH] fix: add guards for MagickImage.Statistic (#1582) --- 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 5f3a926eb0..a0a0e37055 100644 --- a/src/Magick.NET/MagickImage.cs +++ b/src/Magick.NET/MagickImage.cs @@ -6223,7 +6223,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() {