From 2486de5c22f658e071a41d541ff5859171b39924 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 21 Aug 2026 11:32:48 +0000 Subject: [PATCH] fix(types): add input validation to Bitmap constructor Co-authored-by: johnstrand <11484777+johnstrand@users.noreply.github.com> --- src/GameUtils/Types/Bitmap.cs | 26 ++++++++++++++++++---- tests/GameUtils.Tests/Types/BitmapTests.cs | 11 +++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/GameUtils/Types/Bitmap.cs b/src/GameUtils/Types/Bitmap.cs index 8a1abc2..71a9f5f 100644 --- a/src/GameUtils/Types/Bitmap.cs +++ b/src/GameUtils/Types/Bitmap.cs @@ -1,3 +1,4 @@ +using System; using System.Numerics; namespace GameUtils.Types; @@ -5,22 +6,39 @@ namespace GameUtils.Types; /// /// Bitmap image class /// -public class Bitmap(int width, int height) +public class Bitmap { /// /// Width of the image in pixels /// - public int Width { get; } = width; + public int Width { get; } /// /// Height of the image in pixels /// - public int Height { get; } = height; + public int Height { get; } /// /// Pixel data /// - public Vector3[] Data { get; } = new Vector3[width * height]; + public Vector3[] Data { get; } + + /// + /// Initializes a new instance of the class with the specified width and height. + /// + public Bitmap(int width, int height) + { + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(width); + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(height); + if ((long)width * height > int.MaxValue) + { + throw new ArgumentOutOfRangeException(nameof(height), "Width and height product exceeds maximum array size."); + } + + Width = width; + Height = height; + Data = new Vector3[width * height]; + } /// /// Gets or sets a pixel at the specified coordinates. If the coordinates are out of bounds, no operation is performed and Vector3.Zero is returned. diff --git a/tests/GameUtils.Tests/Types/BitmapTests.cs b/tests/GameUtils.Tests/Types/BitmapTests.cs index 73e49c7..4144f20 100644 --- a/tests/GameUtils.Tests/Types/BitmapTests.cs +++ b/tests/GameUtils.Tests/Types/BitmapTests.cs @@ -17,6 +17,17 @@ public void Write_WithTraversalPath_ThrowsUnauthorizedAccessException() Assert.ThrowsExactly(() => bitmap.Write(invalidPath)); } + [TestMethod] + [DataRow(-1, 10)] + [DataRow(0, 10)] + [DataRow(10, -1)] + [DataRow(10, 0)] + [DataRow(100000, 100000)] + public void Constructor_InvalidDimensions_ThrowsArgumentOutOfRangeException(int width, int height) + { + Assert.ThrowsExactly(() => new Bitmap(width, height)); + } + [TestMethod] public void Write_WithValidPath_DoesNotThrow() {