From 177fca49175449705812fb20b6dd5b7bcbb5a901 Mon Sep 17 00:00:00 2001 From: Hugh Bellamy Date: Mon, 2 Oct 2017 15:15:28 +0100 Subject: [PATCH] Fix LinearGradientMode parameter validation to match corefx (#5672) --- .../LinearGradientBrush.cs | 24 +++++++++++++ .../LinearGradientBrushTest.cs | 36 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/mcs/class/System.Drawing/System.Drawing.Drawing2D/LinearGradientBrush.cs b/mcs/class/System.Drawing/System.Drawing.Drawing2D/LinearGradientBrush.cs index 3b006fd5c96..d2e645d2ccb 100644 --- a/mcs/class/System.Drawing/System.Drawing.Drawing2D/LinearGradientBrush.cs +++ b/mcs/class/System.Drawing/System.Drawing.Drawing2D/LinearGradientBrush.cs @@ -67,6 +67,14 @@ namespace System.Drawing.Drawing2D { public LinearGradientBrush (Rectangle rect, Color color1, Color color2, LinearGradientMode linearGradientMode) { + if (linearGradientMode < LinearGradientMode.Horizontal || linearGradientMode > LinearGradientMode.BackwardDiagonal) { + throw new InvalidEnumArgumentException (nameof (linearGradientMode), unchecked ((int)linearGradientMode), typeof (LinearGradientMode)); + } + + if (rect.Width == 0 || rect.Height == 0) { + throw new ArgumentException( string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ())); + } + IntPtr nativeObject; Status status = GDIPlus.GdipCreateLineBrushFromRectI (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject); GDIPlus.CheckStatus (status); @@ -81,6 +89,14 @@ namespace System.Drawing.Drawing2D { public LinearGradientBrush (RectangleF rect, Color color1, Color color2, LinearGradientMode linearGradientMode) { + if (linearGradientMode < LinearGradientMode.Horizontal || linearGradientMode > LinearGradientMode.BackwardDiagonal) { + throw new InvalidEnumArgumentException (nameof (linearGradientMode), unchecked ((int)linearGradientMode), typeof (LinearGradientMode)); + } + + if (rect.Width == 0.0 || rect.Height == 0.0) { + throw new ArgumentException (string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ())); + } + IntPtr nativeObject; Status status = GDIPlus.GdipCreateLineBrushFromRect (ref rect, color1.ToArgb (), color2.ToArgb (), linearGradientMode, WrapMode.Tile, out nativeObject); GDIPlus.CheckStatus (status); @@ -95,6 +111,10 @@ namespace System.Drawing.Drawing2D { public LinearGradientBrush (Rectangle rect, Color color1, Color color2, float angle, bool isAngleScaleable) { + if (rect.Width == 0 || rect.Height == 0) { + throw new ArgumentException (string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ())); + } + IntPtr nativeObject; Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngleI (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject); GDIPlus.CheckStatus (status); @@ -105,6 +125,10 @@ namespace System.Drawing.Drawing2D { public LinearGradientBrush (RectangleF rect, Color color1, Color color2, float angle, bool isAngleScaleable) { + if (rect.Width == 0 || rect.Height == 0) { + throw new ArgumentException (string.Format ("Rectangle '{0}' cannot have a width or height equal to 0.", rect.ToString ())); + } + IntPtr nativeObject; Status status = GDIPlus.GdipCreateLineBrushFromRectWithAngle (ref rect, color1.ToArgb (), color2.ToArgb (), angle, isAngleScaleable, WrapMode.Tile, out nativeObject); GDIPlus.CheckStatus (status); diff --git a/mcs/class/System.Drawing/Test/System.Drawing.Drawing2D/LinearGradientBrushTest.cs b/mcs/class/System.Drawing/Test/System.Drawing.Drawing2D/LinearGradientBrushTest.cs index 937c9ebbc79..ebf1f86e98c 100644 --- a/mcs/class/System.Drawing/Test/System.Drawing.Drawing2D/LinearGradientBrushTest.cs +++ b/mcs/class/System.Drawing/Test/System.Drawing.Drawing2D/LinearGradientBrushTest.cs @@ -324,6 +324,42 @@ namespace MonoTests.System.Drawing.Drawing2D { Assert.AreEqual (32, elements[5], 0.0001, "matrix.5"); } + [Test] + public void Constructor_Rectangle_InvalidWidthHeight () + { + var emptyWidth = new Rectangle (0, 0, 0, 1); + var emptyHeight = new Rectangle (0, 0, 0, 1); + + Assert.Throws(() => new LinearGradientBrush (emptyWidth, Color.Empty, Color.Empty, 1)); + Assert.Throws(() => new LinearGradientBrush (emptyHeight, Color.Empty, Color.Empty, 1)); + Assert.Throws(() => new LinearGradientBrush (emptyWidth, Color.Empty, Color.Empty, LinearGradientMode.BackwardDiagonal)); + Assert.Throws(() => new LinearGradientBrush (emptyHeight, Color.Empty, Color.Empty, LinearGradientMode.BackwardDiagonal)); + } + + [Test] + public void Constructor_RectangleF_InvalidWidthHeight () + { + var emptyWidth = new RectangleF (0, 0, 0, 1); + var emptyHeight = new RectangleF (0, 0, 0, 1); + + Assert.Throws(() => new LinearGradientBrush (emptyWidth, Color.Empty, Color.Empty, 1)); + Assert.Throws(() => new LinearGradientBrush (emptyHeight, Color.Empty, Color.Empty, 1)); + Assert.Throws(() => new LinearGradientBrush (emptyWidth, Color.Empty, Color.Empty, LinearGradientMode.BackwardDiagonal)); + Assert.Throws(() => new LinearGradientBrush (emptyHeight, Color.Empty, Color.Empty, LinearGradientMode.BackwardDiagonal)); + } + + [Test] + public void Constructor_LinearGradientMode_InvalidMode () + { + var rect = new Rectangle (0, 0, 1, 1); + var rectf = new RectangleF (0, 0, 1, 1); + + Assert.Throws(() => new LinearGradientBrush (rect, Color.Empty, Color.Empty, LinearGradientMode.Horizontal - 1)); + Assert.Throws(() => new LinearGradientBrush (rectf, Color.Empty, Color.Empty, LinearGradientMode.Horizontal - 1)); + Assert.Throws(() => new LinearGradientBrush (rect, Color.Empty, Color.Empty, LinearGradientMode.BackwardDiagonal + 1)); + Assert.Throws(() => new LinearGradientBrush (rectf, Color.Empty, Color.Empty, LinearGradientMode.BackwardDiagonal + 1)); + } + [Test] public void InterpolationColors_Colors_InvalidBlend () { -- 2.25.1