Skip to content

Commit 386da3f

Browse files
authored
Check max length when writing MemoryStream (#52)
1 parent 311a266 commit 386da3f

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

System.IO.Streams/MemoryStream.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ public override long Seek(
328328
}
329329

330330
/// <inheritdoc/>
331+
/// <exception cref="ObjectDisposedException"></exception>
332+
/// <exception cref="NotSupportedException">The stream buffer does not have the capacity to hold the data and is not expandable.</exception>
333+
/// <exception cref="ArgumentOutOfRangeException">The MemoryStream max size was exceeded</exception>
331334
public override void SetLength(long value)
332335
{
333336
EnsureOpen();
@@ -364,6 +367,12 @@ public virtual byte[] ToArray()
364367
}
365368

366369
/// <inheritdoc/>
370+
/// <exception cref="ObjectDisposedException"></exception>
371+
/// <exception cref="NotSupportedException">The stream buffer does not have the capacity to hold the data and is not expandable.</exception>
372+
/// <exception cref="ArgumentOutOfRangeException">
373+
/// The MemoryStream max size was exceeded or
374+
/// <paramref name="offset"/> or <paramref name="count"/> are less than 0
375+
/// </exception>
367376
public override void Write(byte[] buffer, int offset, int count)
368377
{
369378
EnsureOpen();
@@ -401,6 +410,9 @@ public override void Write(byte[] buffer, int offset, int count)
401410
}
402411

403412
/// <inheritdoc/>
413+
/// <exception cref="ObjectDisposedException"></exception>
414+
/// <exception cref="NotSupportedException">The stream buffer does not have the capacity to hold the data and is not expandable.</exception>
415+
/// <exception cref="ArgumentOutOfRangeException">The MemoryStream max size was exceeded</exception>
404416
public override void WriteByte(byte value)
405417
{
406418
EnsureOpen();
@@ -452,7 +464,8 @@ private void EnsureOpen()
452464
/// </summary>
453465
/// <param name="value">Value for the new capacity.</param>
454466
/// <returns><see langword="true"/> if allocation for a new array was successful. <see langword="false"/> otherwise.</returns>
455-
/// <exception cref="NotSupportedException"></exception>
467+
/// <exception cref="NotSupportedException">The stream buffer does not have the capacity to hold the data and is not expandable.</exception>
468+
/// <exception cref="ArgumentOutOfRangeException">The MemoryStream max size would be exceeded by resizing</exception>
456469
private bool EnsureCapacity(int value)
457470
{
458471
if (value > _capacity)
@@ -464,11 +477,21 @@ private bool EnsureCapacity(int value)
464477
newCapacity = CapacityDefaultSize;
465478
}
466479

480+
if (value > MemStreamMaxLength)
481+
{
482+
throw new ArgumentOutOfRangeException();
483+
}
484+
467485
if (newCapacity < _capacity * 2)
468486
{
469487
newCapacity = _capacity * 2;
470488
}
471489

490+
if (newCapacity > MemStreamMaxLength)
491+
{
492+
newCapacity = MemStreamMaxLength;
493+
}
494+
472495
if (!_expandable && newCapacity > _capacity)
473496
{
474497
throw new NotSupportedException();

UnitTests/MemoryStreamUnitTests/Write.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,30 @@ public void BoundaryCheck()
385385
}
386386
}
387387

388+
[TestMethod]
389+
public void Write_AtMaxLength_Works()
390+
{
391+
var maxLength = 0xFFFF;
392+
using (var ms = new MemoryStream())
393+
{
394+
ms.Position = maxLength - 1;
395+
// should resize the buffer to the max size
396+
ms.WriteByte(0);
397+
}
398+
}
399+
400+
[TestMethod]
401+
public void Write_AboveMaxLength_Throws()
402+
{
403+
var maxLength = 0xFFFF;
404+
using (var ms = new MemoryStream())
405+
{
406+
ms.Position = maxLength;
407+
// should throw as the max size is exceeded
408+
Assert.ThrowsException(typeof(ArgumentOutOfRangeException), () => ms.WriteByte(0));
409+
}
410+
}
411+
388412
#endregion Test Cases
389413
}
390414
}

0 commit comments

Comments
 (0)