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)