From e715ceddba2a408bc2fb3a6e150513fe3acde6fa Mon Sep 17 00:00:00 2001 From: alinpahontu2912 Date: Wed, 28 Jan 2026 10:41:02 +0100 Subject: [PATCH 1/4] add maximum boundary check for memorystream capacity --- .../System.Private.CoreLib/src/System/IO/MemoryStream.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs b/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs index 53f717dfe80a4e..6aa410fce3e751 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs @@ -263,6 +263,8 @@ public virtual int Capacity // Special behavior if the MS isn't expandable: we don't throw if value is the same as the current capacity if (value < Length) throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_SmallCapacity); + if (value > MemStreamMaxLength) + throw new ArgumentOutOfRangeException(nameof(value), SR.Format(SR.ArgumentOutOfRange_StreamLength, Array.MaxLength)); EnsureNotClosed(); From 5b2f5e9fe7d4e3611a12f81a815f83ba4b2b4b4e Mon Sep 17 00:00:00 2001 From: Stefan-Alin Pahontu <56953855+alinpahontu2912@users.noreply.github.com> Date: Wed, 28 Jan 2026 17:01:33 +0100 Subject: [PATCH 2/4] Update src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../System.Private.CoreLib/src/System/IO/MemoryStream.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs b/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs index 6aa410fce3e751..8e4a72439b53dd 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs @@ -264,7 +264,7 @@ public virtual int Capacity if (value < Length) throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_SmallCapacity); if (value > MemStreamMaxLength) - throw new ArgumentOutOfRangeException(nameof(value), SR.Format(SR.ArgumentOutOfRange_StreamLength, Array.MaxLength)); + throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_Capacity); EnsureNotClosed(); From 77e51ca32e63232a4331a5a17d69950dbe8e531f Mon Sep 17 00:00:00 2001 From: Stefan-Alin Pahontu <56953855+alinpahontu2912@users.noreply.github.com> Date: Thu, 5 Feb 2026 11:32:48 +0100 Subject: [PATCH 3/4] revert change and fix exception type --- .../System.Private.CoreLib/src/System/IO/MemoryStream.cs | 2 -- .../tests/System.IO.Tests/MemoryStream/MemoryStreamTests.cs | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs b/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs index 8e4a72439b53dd..53f717dfe80a4e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs @@ -263,8 +263,6 @@ public virtual int Capacity // Special behavior if the MS isn't expandable: we don't throw if value is the same as the current capacity if (value < Length) throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_SmallCapacity); - if (value > MemStreamMaxLength) - throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_Capacity); EnsureNotClosed(); diff --git a/src/libraries/System.Runtime/tests/System.IO.Tests/MemoryStream/MemoryStreamTests.cs b/src/libraries/System.Runtime/tests/System.IO.Tests/MemoryStream/MemoryStreamTests.cs index ee77811f224290..a6cac02f63a2e3 100644 --- a/src/libraries/System.Runtime/tests/System.IO.Tests/MemoryStream/MemoryStreamTests.cs +++ b/src/libraries/System.Runtime/tests/System.IO.Tests/MemoryStream/MemoryStreamTests.cs @@ -160,9 +160,9 @@ public void MemoryStream_CapacityBoundaryChecks() ms.Capacity = MaxSupportedLength; Assert.Equal(MaxSupportedLength, ms.Capacity); - Assert.Throws(() => ms.Capacity = MaxSupportedLength + 1); + Assert.Throws(() => ms.Capacity = MaxSupportedLength + 1); - Assert.Throws(() => ms.Capacity = int.MaxValue); + Assert.Throws(() => ms.Capacity = int.MaxValue); } } From b10484df2fb5fe23775ce6a68c8ff44fea2d7e98 Mon Sep 17 00:00:00 2001 From: Stefan-Alin Pahontu <56953855+alinpahontu2912@users.noreply.github.com> Date: Thu, 12 Feb 2026 10:06:56 +0100 Subject: [PATCH 4/4] remove check with unwanted exceotion type --- .../System.Private.CoreLib/src/System/IO/MemoryStream.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs b/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs index 53f717dfe80a4e..31a5fc7513ae67 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/MemoryStream.cs @@ -44,7 +44,6 @@ public MemoryStream() public MemoryStream(int capacity) { ArgumentOutOfRangeException.ThrowIfNegative(capacity); - ArgumentOutOfRangeException.ThrowIfGreaterThan(capacity, MemStreamMaxLength); _buffer = capacity != 0 ? new byte[capacity] : Array.Empty(); _capacity = capacity;