Skip to content

Commit

Permalink
Turn on argument exception analyzer on runtime, fix warnings found (d…
Browse files Browse the repository at this point in the history
…otnet/runtime#38578)

* Turn on argument exception analyzer on runtime, fix failures found

Commit migrated from dotnet/runtime@06fa941
  • Loading branch information
buyaa-n authored Jul 2, 2020
1 parent 174f40a commit 0024918
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Bitmap(Stream stream, bool useIcm)
public Bitmap(Type type, string resource)
{
if (resource == null)
throw new ArgumentException(nameof(resource));
throw new ArgumentNullException(nameof(resource));

// For compatibility with the .NET Framework
if (type == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public sealed partial class Bitmap
{
public Bitmap(Type type, string resource)
{
if (resource == null)
throw new ArgumentNullException(nameof(resource));

Stream? stream = type.Module.Assembly.GetManifestResourceStream(type, resource);
if (stream == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ public void AddLines(Point[] points)
if (points == null)
throw new ArgumentNullException(nameof(points));
if (points.Length == 0)
throw new ArgumentException(nameof(points));
throw new ArgumentException(null, nameof(points));

int status = Gdip.GdipAddPathLine2I(_nativePath, points, points.Length);
Gdip.CheckStatus(status);
Expand All @@ -392,7 +392,7 @@ public void AddLines(PointF[] points)
if (points == null)
throw new ArgumentNullException(nameof(points));
if (points.Length == 0)
throw new ArgumentException(nameof(points));
throw new ArgumentException(null, nameof(points));

int status = Gdip.GdipAddPathLine2(_nativePath, points, points.Length);
Gdip.CheckStatus(status);
Expand Down Expand Up @@ -464,7 +464,7 @@ public void AddRectangles(Rectangle[] rects)
if (rects == null)
throw new ArgumentNullException(nameof(rects));
if (rects.Length == 0)
throw new ArgumentException(nameof(rects));
throw new ArgumentException(null, nameof(rects));

int status = Gdip.GdipAddPathRectanglesI(_nativePath, rects, rects.Length);
Gdip.CheckStatus(status);
Expand All @@ -475,7 +475,7 @@ public void AddRectangles(RectangleF[] rects)
if (rects == null)
throw new ArgumentNullException(nameof(rects));
if (rects.Length == 0)
throw new ArgumentException(nameof(rects));
throw new ArgumentException(null, nameof(rects));

int status = Gdip.GdipAddPathRectangles(_nativePath, rects, rects.Length);
Gdip.CheckStatus(status);
Expand Down Expand Up @@ -642,7 +642,7 @@ public void AddString(string s, FontFamily family, int style, float emSize, Poin
public void AddString(string s, FontFamily family, int style, float emSize, Rectangle layoutRect, StringFormat? format)
{
if (family == null)
throw new ArgumentException(nameof(family));
throw new ArgumentNullException(nameof(family));

IntPtr sformat = (format == null) ? IntPtr.Zero : format.nativeFormat;
// note: the NullReferenceException on s.Length is the expected (MS) exception
Expand All @@ -653,7 +653,7 @@ public void AddString(string s, FontFamily family, int style, float emSize, Rect
public void AddString(string s, FontFamily family, int style, float emSize, RectangleF layoutRect, StringFormat? format)
{
if (family == null)
throw new ArgumentException(nameof(family));
throw new ArgumentNullException(nameof(family));

IntPtr sformat = (format == null) ? IntPtr.Zero : format.nativeFormat;
// note: the NullReferenceException on s.Length is the expected (MS) exception
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ public unsafe void AddLines(PointF[] points)
{
if (points == null)
throw new ArgumentNullException(nameof(points));
if (points.Length == 0)
throw new ArgumentException(null, nameof(points));

fixed (PointF* p = points)
{
Expand All @@ -312,6 +314,8 @@ public unsafe void AddLines(Point[] points)
{
if (points == null)
throw new ArgumentNullException(nameof(points));
if (points.Length == 0)
throw new ArgumentException(null, nameof(points));

fixed (Point* p = points)
{
Expand Down Expand Up @@ -526,6 +530,8 @@ public unsafe void AddRectangles(RectangleF[] rects)
{
if (rects == null)
throw new ArgumentNullException(nameof(rects));
if (rects.Length == 0)
throw new ArgumentException(null, nameof(rects));

fixed (RectangleF* r = rects)
{
Expand All @@ -545,6 +551,8 @@ public unsafe void AddRectangles(Rectangle[] rects)
{
if (rects == null)
throw new ArgumentNullException(nameof(rects));
if (rects.Length == 0)
throw new ArgumentException(null, nameof(rects));

fixed (Rectangle* r = rects)
{
Expand Down Expand Up @@ -639,6 +647,9 @@ public void AddString(string s, FontFamily family, int style, float emSize, Poin

public void AddString(string s, FontFamily family, int style, float emSize, RectangleF layoutRect, StringFormat? format)
{
if (family == null)
throw new ArgumentNullException(nameof(family));

Gdip.CheckStatus(Gdip.GdipAddPathString(
new HandleRef(this, _nativePath),
s,
Expand All @@ -652,6 +663,9 @@ public void AddString(string s, FontFamily family, int style, float emSize, Rect

public void AddString(string s, FontFamily family, int style, float emSize, Rectangle layoutRect, StringFormat? format)
{
if (family == null)
throw new ArgumentNullException(nameof(family));

Gdip.CheckStatus(Gdip.GdipAddPathStringI(
new HandleRef(this, _nativePath),
s,
Expand Down
8 changes: 4 additions & 4 deletions src/System.Drawing.Common/src/System/Drawing/Icon.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public Icon(Icon original, Size size)
}

if (id == ushort.MaxValue)
throw new ArgumentException(SR.NoValidIconImageFound, "Icon");
throw new ArgumentException(SR.NoValidIconImageFound, nameof(original));

iconSize.Height = iconDir.idEntries[id].height;
iconSize.Width = iconDir.idEntries[id].width;
Expand Down Expand Up @@ -240,7 +240,7 @@ public Icon(string fileName)
public Icon(Type type, string resource)
{
if (resource == null)
throw new ArgumentException(nameof(resource));
throw new ArgumentNullException(nameof(resource));

// For compatibility with the .NET Framework
if (type == null)
Expand Down Expand Up @@ -313,7 +313,7 @@ public static Icon ExtractAssociatedIcon(string filePath)
if (filePath == null)
throw new ArgumentNullException(nameof(filePath));
if (string.IsNullOrEmpty(filePath))
throw new ArgumentException(SR.NullOrEmptyPath, "path");
throw new ArgumentException(SR.NullOrEmptyPath, nameof(filePath));
if (!File.Exists(filePath))
throw new FileNotFoundException(SR.CouldntFindSpecifiedFile, filePath);

Expand Down Expand Up @@ -346,7 +346,7 @@ public object Clone()
public static Icon FromHandle(IntPtr handle)
{
if (handle == IntPtr.Zero)
throw new ArgumentException(nameof(handle));
throw new ArgumentException(null, nameof(handle));

return new Icon(handle);
}
Expand Down
14 changes: 13 additions & 1 deletion src/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ public Icon(Icon original, int width, int height) : this()

public Icon(Type type, string resource) : this()
{
if (resource == null)
throw new ArgumentNullException(nameof(resource));

Stream? stream = type.Module.Assembly.GetManifestResourceStream(type, resource);
if (stream == null)
{
Expand Down Expand Up @@ -167,6 +170,9 @@ void ISerializable.GetObjectData(SerializationInfo si, StreamingContext context)
throw new ArgumentNullException(nameof(filePath));
}

if (string.IsNullOrEmpty(filePath))
throw new ArgumentException(SR.NullOrEmptyPath, nameof(filePath));

filePath = Path.GetFullPath(filePath);
if (!File.Exists(filePath))
{
Expand Down Expand Up @@ -440,7 +446,13 @@ internal void DrawUnstretched(Graphics graphics, Rectangle targetRect)

~Icon() => Dispose(false);

public static Icon FromHandle(IntPtr handle) => new Icon(handle);
public static Icon FromHandle(IntPtr handle)
{
if (handle == IntPtr.Zero)
throw new ArgumentException(null, nameof(handle));

return new Icon(handle);
}

// Initializes this Image object. This is identical to calling the image's
// constructor with picture, but this allows non-constructor initialization,
Expand Down
7 changes: 6 additions & 1 deletion src/System.Drawing.Common/tests/BitmapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ public void Ctor_NullType_ThrowsNullReferenceException()

[ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)]
[ConditionalTheory(Helpers.IsDrawingSupported)]
[InlineData(typeof(Bitmap), null)]
[InlineData(typeof(Bitmap), "")]
[InlineData(typeof(Bitmap), "bitmap_173x183_indexed_8bit.bmp")]
[InlineData(typeof(BitmapTests), "bitmap_173x183_INDEXED_8bit.bmp")]
Expand All @@ -121,6 +120,12 @@ public void Ctor_InvalidResource_ThrowsArgumentException(Type type, string resou
AssertExtensions.Throws<ArgumentException>(null, () => new Bitmap(type, resource));
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void Ctor_InvalidResource_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException, ArgumentException>("resource", null, () => new Bitmap(typeof(Bitmap), null));
}

[ConditionalTheory(Helpers.IsDrawingSupported)]
[MemberData(nameof(Ctor_FilePath_TestData))]
public void Ctor_Stream(string filename, int width, int height, PixelFormat pixelFormat, ImageFormat rawFormat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ public void AddLines_PointsNull_ThrowsArgumentNullException()
[ConditionalFact(Helpers.IsDrawingSupported)]
public void AddLines_ZeroPoints_ThrowsArgumentException()
{
AssertExtensions.Throws<ArgumentException>(null, () => new GraphicsPath().AddLines(new Point[0]));
AssertExtensions.Throws<ArgumentException>(null, () => new GraphicsPath().AddLines(new PointF[0]));
AssertExtensions.Throws<ArgumentException>("points", null, () => new GraphicsPath().AddLines(new Point[0]));
AssertExtensions.Throws<ArgumentException>("points", null, () => new GraphicsPath().AddLines(new PointF[0]));
}

[ActiveIssue("https://github.com/dotnet/runtime/issues/22221", TestPlatforms.AnyUnix)]
Expand Down Expand Up @@ -1262,11 +1262,11 @@ public void AddString_StringNull_ThrowsNullReferenceException()
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void AddString_FontFamilyNull_ThrowsArgumentException()
public void AddString_FontFamilyNull_ThrowsArgumentNullException()
{
using (GraphicsPath gp = new GraphicsPath())
{
AssertExtensions.Throws<ArgumentException>(null, () =>
AssertExtensions.Throws<ArgumentNullException, ArgumentException>("family", null, () =>
new GraphicsPath().AddString("mono", null, 0, 10, new Point(10, 10), StringFormat.GenericDefault));
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/System.Drawing.Common/tests/IconTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ public void Ctor_NullType_ThrowsNullReferenceException()
}

[ConditionalTheory(Helpers.IsDrawingSupported)]
[InlineData(typeof(Icon), null)]
[InlineData(typeof(Icon), "")]
[InlineData(typeof(Icon), "48x48_multiple_entries_4bit.ico")]
[InlineData(typeof(IconTests), "48x48_MULTIPLE_entries_4bit.ico")]
Expand All @@ -292,6 +291,12 @@ public void Ctor_InvalidResource_ThrowsArgumentException(Type type, string resou
AssertExtensions.Throws<ArgumentException>(null, () => new Icon(type, resource));
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void Ctor_InvalidResource_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException, ArgumentException>("resource", null, () => new Icon(typeof(Icon), null));
}

[ConditionalFact(Helpers.IsDrawingSupported)]
public void Clone_ConstructedIcon_Success()
{
Expand Down Expand Up @@ -437,7 +442,7 @@ public void ExtractAssociatedIcon_NullFilePath_ThrowsArgumentNullException()
[ConditionalFact(Helpers.IsDrawingSupported)]
public void ExtractAssociatedIcon_InvalidFilePath_ThrowsArgumentException()
{
AssertExtensions.Throws<ArgumentException>("path", null, () => Icon.ExtractAssociatedIcon(""));
AssertExtensions.Throws<ArgumentException>("filePath", null, () => Icon.ExtractAssociatedIcon(""));
}


Expand Down Expand Up @@ -759,7 +764,7 @@ public void FromHandle_BitmapHandleMultipleTime_Success()
[ConditionalFact(Helpers.IsDrawingSupported)]
public void FromHandle_Zero_ThrowsArgumentException()
{
AssertExtensions.Throws<ArgumentException>(null, () => Icon.FromHandle(IntPtr.Zero));
AssertExtensions.Throws<ArgumentException>("handle", null, () => Icon.FromHandle(IntPtr.Zero));
}

[ConditionalFact(Helpers.IsDrawingSupported)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ public void BitmapTypeStringCtor1()
[ConditionalFact(Helpers.IsDrawingSupported)]
public void BitmapTypeStringCtor2()
{
Assert.Throws<ArgumentException>(() => new Bitmap(typeof(Bitmap), null));
AssertExtensions.Throws<ArgumentNullException, ArgumentException>("resource", null, () => new Bitmap(typeof(Bitmap), null));
}

private void SetResolution(float x, float y)
Expand Down

0 comments on commit 0024918

Please sign in to comment.