From 64463fe9319c0f578736622c9dda7f80410d1226 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Mon, 15 Nov 2021 17:44:03 -0500 Subject: [PATCH] Add ThrowIfNull overload for pointers --- .../src/System/ArgumentNullException.cs | 12 +++++++++++ .../System.Runtime/ref/System.Runtime.cs | 2 ++ .../System/ArgumentNullExceptionTests.cs | 20 +++++++++++++------ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cs b/src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cs index 5f8894e291473e..46b7c66cf6dfae 100644 --- a/src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cs +++ b/src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cs @@ -64,6 +64,18 @@ public static void ThrowIfNull([NotNull] object? argument, [CallerArgumentExpres } } + /// Throws an if is null. + /// The pointer argument to validate as non-null. + /// The name of the parameter with which corresponds. + [CLSCompliant(false)] + public static unsafe void ThrowIfNull([NotNull] void* argument, [CallerArgumentExpression("argument")] string? paramName = null) + { + if (argument is null) + { + Throw(paramName); + } + } + [DoesNotReturn] private static void Throw(string? paramName) => throw new ArgumentNullException(paramName); diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index bd2e0d81869b2b..f49a99fc1d1253 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -285,6 +285,8 @@ public ArgumentNullException(string? paramName) { } public ArgumentNullException(string? message, System.Exception? innerException) { } public ArgumentNullException(string? paramName, string? message) { } public static void ThrowIfNull([System.Diagnostics.CodeAnalysis.NotNullAttribute] object? argument, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute("argument")] string? paramName = null) { throw null; } + [System.CLSCompliant(false)] + public static unsafe void ThrowIfNull([System.Diagnostics.CodeAnalysis.NotNullAttribute] void* argument, [System.Runtime.CompilerServices.CallerArgumentExpressionAttribute("argument")] string? paramName = null) { throw null; } } public partial class ArgumentOutOfRangeException : System.ArgumentException { diff --git a/src/libraries/System.Runtime/tests/System/ArgumentNullExceptionTests.cs b/src/libraries/System.Runtime/tests/System/ArgumentNullExceptionTests.cs index 3a5e3ad675b938..9705f44c55c14a 100644 --- a/src/libraries/System.Runtime/tests/System/ArgumentNullExceptionTests.cs +++ b/src/libraries/System.Runtime/tests/System/ArgumentNullExceptionTests.cs @@ -49,29 +49,37 @@ public static void Ctor_String_String() } [Fact] - public static void ThrowIfNull_NonNull_DoesntThrow() + public static unsafe void ThrowIfNull_NonNull_DoesntThrow() { foreach (object o in new[] { new object(), "", "argument" }) { ArgumentNullException.ThrowIfNull(o); ArgumentNullException.ThrowIfNull(o, "paramName"); } + + int i = 0; + ArgumentNullException.ThrowIfNull(&i); + ArgumentNullException.ThrowIfNull(&i, "paramName"); } [Theory] [InlineData(null)] [InlineData("")] [InlineData("name")] - public static void ThrowIfNull_Null_ThrowsArgumentNullException(string paramName) + public static unsafe void ThrowIfNull_Null_ThrowsArgumentNullException(string paramName) { - AssertExtensions.Throws(paramName, () => ArgumentNullException.ThrowIfNull(null, paramName)); + AssertExtensions.Throws(paramName, () => ArgumentNullException.ThrowIfNull((object)null, paramName)); + AssertExtensions.Throws(paramName, () => ArgumentNullException.ThrowIfNull((void*)null, paramName)); } [Fact] - public static void ThrowIfNull_UsesArgumentExpression() + public static unsafe void ThrowIfNull_UsesArgumentExpression() { - object something = null; - AssertExtensions.Throws(nameof(something), () => ArgumentNullException.ThrowIfNull(something)); + object someObject = null; + AssertExtensions.Throws(nameof(someObject), () => ArgumentNullException.ThrowIfNull(someObject)); + + byte* somePointer = null; + AssertExtensions.Throws(nameof(somePointer), () => ArgumentNullException.ThrowIfNull(somePointer)); } } }