From ce1310074786630235cc326e0438d2cf38b5aae6 Mon Sep 17 00:00:00 2001 From: Levi Broderick Date: Thu, 11 Apr 2019 11:23:53 -0700 Subject: [PATCH] PR feedback: Validate nint definitions --- src/System.Private.CoreLib/shared/System/Text/ASCIIUtility.cs | 8 ++++++++ .../shared/System/Text/Unicode/Utf16Utility.Validation.cs | 8 ++++++++ .../shared/System/Text/Unicode/Utf8Utility.Transcoding.cs | 10 ++++++++++ .../shared/System/Text/Unicode/Utf8Utility.Validation.cs | 8 ++++++++ 4 files changed, 34 insertions(+) diff --git a/src/System.Private.CoreLib/shared/System/Text/ASCIIUtility.cs b/src/System.Private.CoreLib/shared/System/Text/ASCIIUtility.cs index 18a668a..8ff5b05 100644 --- a/src/System.Private.CoreLib/shared/System/Text/ASCIIUtility.cs +++ b/src/System.Private.CoreLib/shared/System/Text/ASCIIUtility.cs @@ -21,6 +21,14 @@ namespace System.Text { internal static partial class ASCIIUtility { +#if DEBUG + static ASCIIUtility() + { + Debug.Assert(sizeof(nint) == IntPtr.Size && nint.MinValue < 0, "nint is defined incorrectly."); + Debug.Assert(sizeof(nuint) == IntPtr.Size && nuint.MinValue == 0, "nuint is defined incorrectly."); + } +#endif // DEBUG + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static bool AllBytesInUInt64AreAscii(ulong value) { diff --git a/src/System.Private.CoreLib/shared/System/Text/Unicode/Utf16Utility.Validation.cs b/src/System.Private.CoreLib/shared/System/Text/Unicode/Utf16Utility.Validation.cs index 878e593..5f044b1 100644 --- a/src/System.Private.CoreLib/shared/System/Text/Unicode/Utf16Utility.Validation.cs +++ b/src/System.Private.CoreLib/shared/System/Text/Unicode/Utf16Utility.Validation.cs @@ -20,6 +20,14 @@ namespace System.Text.Unicode { internal static unsafe partial class Utf16Utility { +#if DEBUG + static Utf16Utility() + { + Debug.Assert(sizeof(nint) == IntPtr.Size && nint.MinValue < 0, "nint is defined incorrectly."); + Debug.Assert(sizeof(nuint) == IntPtr.Size && nuint.MinValue == 0, "nuint is defined incorrectly."); + } +#endif // DEBUG + // Returns &inputBuffer[inputLength] if the input buffer is valid. /// /// Given an input buffer of char length , diff --git a/src/System.Private.CoreLib/shared/System/Text/Unicode/Utf8Utility.Transcoding.cs b/src/System.Private.CoreLib/shared/System/Text/Unicode/Utf8Utility.Transcoding.cs index c160b1b..9ee331c 100644 --- a/src/System.Private.CoreLib/shared/System/Text/Unicode/Utf8Utility.Transcoding.cs +++ b/src/System.Private.CoreLib/shared/System/Text/Unicode/Utf8Utility.Transcoding.cs @@ -22,6 +22,16 @@ namespace System.Text.Unicode { internal static unsafe partial class Utf8Utility { +#if DEBUG + static Utf8Utility() + { + Debug.Assert(sizeof(nint) == IntPtr.Size && nint.MinValue < 0, "nint is defined incorrectly."); + Debug.Assert(sizeof(nuint) == IntPtr.Size && nuint.MinValue == 0, "nuint is defined incorrectly."); + + _ValidateAdditionalNIntDefinitions(); + } +#endif // DEBUG + // On method return, pInputBufferRemaining and pOutputBufferRemaining will both point to where // the next byte would have been consumed from / the next char would have been written to. // inputLength in bytes, outputCharsRemaining in chars. diff --git a/src/System.Private.CoreLib/shared/System/Text/Unicode/Utf8Utility.Validation.cs b/src/System.Private.CoreLib/shared/System/Text/Unicode/Utf8Utility.Validation.cs index 8ef9369..6425ae1 100644 --- a/src/System.Private.CoreLib/shared/System/Text/Unicode/Utf8Utility.Validation.cs +++ b/src/System.Private.CoreLib/shared/System/Text/Unicode/Utf8Utility.Validation.cs @@ -19,6 +19,14 @@ namespace System.Text.Unicode { internal static unsafe partial class Utf8Utility { +#if DEBUG + private static void _ValidateAdditionalNIntDefinitions() + { + Debug.Assert(sizeof(nint) == IntPtr.Size && nint.MinValue < 0, "nint is defined incorrectly."); + Debug.Assert(sizeof(nuint) == IntPtr.Size && nuint.MinValue == 0, "nuint is defined incorrectly."); + } +#endif // DEBUG + // Returns &inputBuffer[inputLength] if the input buffer is valid. /// /// Given an input buffer of byte length , -- 2.7.4