From b043e5eb8165b5c0ed52ec3d606a88a7e9cf961a Mon Sep 17 00:00:00 2001 From: Lehonti Ramos Date: Mon, 7 Aug 2023 20:59:21 +0200 Subject: [PATCH] Made `BitMask` reject negative indexing --- Pinta.Core/Effects/BitMask.cs | 9 +++++- tests/Pinta.Core.Tests/BitMaskTest.cs | 46 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/Pinta.Core/Effects/BitMask.cs b/Pinta.Core/Effects/BitMask.cs index ecf8830c9..d8b1209dd 100644 --- a/Pinta.Core/Effects/BitMask.cs +++ b/Pinta.Core/Effects/BitMask.cs @@ -69,5 +69,12 @@ public void Set (RectangleI rect, bool newValue) Set (x, y, newValue); } - private int GetIndex (int x, int y) => (y * Width) + x; + private int GetIndex (int x, int y) + { + if (x < 0) + throw new ArgumentOutOfRangeException (nameof(x)); + if (y < 0) + throw new ArgumentOutOfRangeException (nameof (y)); + return (y * Width) + x; + } } diff --git a/tests/Pinta.Core.Tests/BitMaskTest.cs b/tests/Pinta.Core.Tests/BitMaskTest.cs index 2dda90acd..58fa731d4 100644 --- a/tests/Pinta.Core.Tests/BitMaskTest.cs +++ b/tests/Pinta.Core.Tests/BitMaskTest.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Gtk; using NUnit.Framework; namespace Pinta.Core.Tests; @@ -103,6 +104,44 @@ public void BitGetsSetXY (int maskWidth, int maskHeight, int bitToSetX, int bitT } } + [TestCaseSource (nameof (invalid_indexing))] + public void RejectsInvalidIndexing_PairIndexer (int width, int height, int x, int y) + { + var bitmask = new BitMask (width, height); + Assert.Throws (() => _ = bitmask[x, y]); + } + + [TestCaseSource (nameof (invalid_indexing))] + public void RejectsInvalidIndexing_PointIndexer (int width, int height, int x, int y) + { + var bitmask = new BitMask (width, height); + var point = new PointI (x, y); + Assert.Throws (() => _ = bitmask[point]); + } + + [TestCaseSource (nameof (invalid_indexing))] + public void RejectsInvalidIndexing_GetMethod (int width, int height, int x, int y) + { + var bitmask = new BitMask (width, height); + Assert.Throws (() => _ = bitmask.Get (x, y)); + } + + [TestCaseSource (nameof (invalid_indexing))] + public void RejectsInvalidIndexing_Invert (int width, int height, int x, int y) + { + var bitmask = new BitMask (width, height); + Assert.Throws (() => bitmask.Invert (x, y)); + } + + [TestCaseSource (nameof (invalid_indexing))] + public void RejectsInvalidIndexing_SetPair (int width, int height, int x, int y) + { + var bitmask1 = new BitMask (width, height); + var bitmask2 = new BitMask (width, height); + Assert.Throws (() => bitmask1.Set (x, y, true)); + Assert.Throws (() => bitmask2.Set (x, y, false)); + } + static readonly IReadOnlyList out_of_bounds_access_cases = new TestCaseData[] { new (0, 0), @@ -122,4 +161,11 @@ public void BitGetsSetXY (int maskWidth, int maskHeight, int bitToSetX, int bitT new (DEFAULT_SIZE, DEFAULT_OFFSET), new (2, 1), }; + + static readonly IReadOnlyList invalid_indexing = new TestCaseData[] + { + new (DEFAULT_WIDTH, DEFAULT_HEIGHT, -1, 0), + new (DEFAULT_WIDTH, DEFAULT_HEIGHT, 0, -1), + new (DEFAULT_WIDTH, DEFAULT_HEIGHT, -1, -1), + }; }