Skip to content

Commit

Permalink
issue-50839 handle large int write input. (#53338)
Browse files Browse the repository at this point in the history
* issue-50839 handle large int write input.

* Also fix Write(ReadOnlySpan); Remove test case because it allocates large object

* Fix comment: add tests.

* Fix comment: use get_position to return _pos; Fix indentation issue.

* Fix spacing

Co-authored-by: Jeff Handley <jeffhandley@users.noreply.github.com>
Co-authored-by: Jeff Handley <jeff.handley@microsoft.com>
  • Loading branch information
3 people authored Jul 30, 2021
1 parent 668a39e commit 5058ca8
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,54 @@ public void BufferSize()
Assert.Equal(1234, bufferedStream.BufferSize);
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.Is64BitProcess))]
[OuterLoop]
[InlineData(int.MaxValue / 2 + 1)]
public void WriteFromByte_InputSizeLargerThanHalfOfMaxInt_ShouldSuccess(int inputSize)
{
byte[] bytes;

try
{
bytes = new byte[inputSize];
}
catch (OutOfMemoryException)
{
return;
}

var writableStream = new WriteOnlyStream();
using (var bs = new BufferedStream(writableStream))
{
bs.Write(bytes, 0, inputSize);
Assert.Equal(inputSize, writableStream.Position);
}
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.Is64BitProcess))]
[OuterLoop]
[InlineData(int.MaxValue / 2 + 1)]
public void WriteFromSpan_InputSizeLargerThanHalfOfMaxInt_ShouldSuccess(int inputSize)
{
byte[] bytes;

try
{
bytes = new byte[inputSize];
}
catch (OutOfMemoryException)
{
return;
}

var writableStream = new WriteOnlyStream();
using (var bs = new BufferedStream(writableStream))
{
bs.Write(new ReadOnlySpan<byte>(bytes));
Assert.Equal(inputSize, writableStream.Position);
}
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down Expand Up @@ -369,4 +417,49 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati
public override Task FlushAsync(CancellationToken cancellationToken) =>
throw new InvalidOperationException("Exception from FlushAsync");
}

internal sealed class WriteOnlyStream : Stream
{
private long _pos;

public override void Flush()
{
}

public override int Read(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}

public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}

public override void SetLength(long value)
{
throw new NotSupportedException();
}

public override void Write(byte[] buffer, int offset, int count)
{
_pos += count;
}

public override void Write(ReadOnlySpan<byte> buffer)
{
_pos += buffer.Length;
}

public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length => _pos;

public override long Position
{
get => _pos;
set => throw new NotSupportedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,8 @@ public override void Write(byte[] buffer, int offset, int count)
checked
{ // We do not expect buffer sizes big enough for an overflow, but if it happens, lets fail early:
totalUserbytes = _writePos + count;
useBuffer = (totalUserbytes + count < (_bufferSize + _bufferSize));
// Allow current totalUserbytes up to int.MaxValue by using uint arithmetic operation for totalUserbytes + count
useBuffer = ((uint)totalUserbytes + count < (_bufferSize + _bufferSize));
}

if (useBuffer)
Expand Down Expand Up @@ -959,7 +960,8 @@ public override void Write(ReadOnlySpan<byte> buffer)
{
// We do not expect buffer sizes big enough for an overflow, but if it happens, lets fail early:
totalUserbytes = _writePos + buffer.Length;
useBuffer = (totalUserbytes + buffer.Length < (_bufferSize + _bufferSize));
// Allow current totalUserbytes up to int.MaxValue by using uint arithmetic operation for totalUserbytes + buffer.Length
useBuffer = ((uint)totalUserbytes + buffer.Length < (_bufferSize + _bufferSize));
}

if (useBuffer)
Expand Down

0 comments on commit 5058ca8

Please sign in to comment.