From 83efef9626644b3243a700b75bb8602b02349637 Mon Sep 17 00:00:00 2001 From: Jeremy Koritzinsky Date: Tue, 25 Sep 2018 16:45:04 -0700 Subject: [PATCH] Simpler implementation from PR feedback. Commit migrated from https://github.com/dotnet/coreclr/commit/35dff1a2277d583b9d554c853bc4b9a31f6f79a0 --- .../src/System/Runtime/InteropServices/SafeBuffer.cs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/SafeBuffer.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/SafeBuffer.cs index 66b500a..74edae3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/SafeBuffer.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/SafeBuffer.cs @@ -110,19 +110,13 @@ namespace System.Runtime.InteropServices [CLSCompliant(false)] public void Initialize(uint numElements, uint sizeOfEachElement) { - if (sizeOfEachElement == 0) + try { - _numBytes = (UIntPtr)0; + _numBytes = checked((UIntPtr)((ulong)numElements * sizeOfEachElement)); } - else + catch (OverflowException) { - if (IntPtr.Size == 4 && numElements > uint.MaxValue / sizeOfEachElement) - throw new ArgumentOutOfRangeException("numBytes", SR.ArgumentOutOfRange_AddressSpace); - - if (numElements >= (ulong)Uninitialized / sizeOfEachElement) - throw new ArgumentOutOfRangeException(nameof(numElements), SR.ArgumentOutOfRange_UIntPtrMax); - - _numBytes = checked((UIntPtr)(numElements * sizeOfEachElement)); + throw new ArgumentOutOfRangeException("numBytes", SR.ArgumentOutOfRange_AddressSpace); } } -- 2.7.4