Simpler implementation from PR feedback.
[platform/upstream/coreclr.git] / src / System.Private.CoreLib / shared / System / Runtime / InteropServices / SafeBuffer.cs
index 661a7ed..74edae3 100644 (file)
@@ -110,13 +110,14 @@ namespace System.Runtime.InteropServices
         [CLSCompliant(false)]
         public void Initialize(uint numElements, uint sizeOfEachElement)
         {
-            if (IntPtr.Size == 4 && numElements * sizeOfEachElement > uint.MaxValue)
+            try
+            {
+                _numBytes = checked((UIntPtr)((ulong)numElements * sizeOfEachElement));
+            }
+            catch (OverflowException)
+            {
                 throw new ArgumentOutOfRangeException("numBytes", SR.ArgumentOutOfRange_AddressSpace);
-
-            if (numElements * sizeOfEachElement >= (ulong)Uninitialized)
-                throw new ArgumentOutOfRangeException(nameof(numElements), SR.ArgumentOutOfRange_UIntPtrMax);
-
-            _numBytes = checked((UIntPtr)(numElements * sizeOfEachElement));
+            }
         }
 
         /// <summary>