From 9a865dd972e7a243a5c328036d21b0d9b9aa1e19 Mon Sep 17 00:00:00 2001 From: Felipe Fujiy Pessoto Date: Tue, 24 Mar 2020 17:43:36 -0300 Subject: [PATCH] Explicitly throw the OutOfMemoryException instead of relying on Shared.Rent/Array constructor. Because it can behave differently in OSX, where it supports int.MaxValue array size. --- .../Text/Json/Serialization/PooledByteBufferWriter.cs | 2 +- .../src/System/Text/Json/ThrowHelper.Serialization.cs | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/PooledByteBufferWriter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/PooledByteBufferWriter.cs index 33cdbcb73fefe..a1a9f09770f6b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/PooledByteBufferWriter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/PooledByteBufferWriter.cs @@ -146,7 +146,7 @@ private void CheckAndResizeBuffer(int sizeHint) newSize = currentLength + sizeHint; if ((uint)newSize > int.MaxValue) { - newSize = int.MaxValue; + ThrowHelper.ThrowOutOfMemoryException_BufferMaximumSizeExceeded((uint)newSize); } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs index 6ff469e1616d0..39937dbf1b89b 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs @@ -12,6 +12,13 @@ namespace System.Text.Json { internal static partial class ThrowHelper { + [DoesNotReturn] + [MethodImpl(MethodImplOptions.NoInlining)] + public static void ThrowOutOfMemoryException_BufferMaximumSizeExceeded(uint capacity) + { + throw new OutOfMemoryException(SR.Format(SR.BufferMaximumSizeExceeded, capacity)); + } + [DoesNotReturn] [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowArgumentException_DeserializeWrongType(Type type, object value)