Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-50839 handle large int write input. #53338

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
jeffhandley marked this conversation as resolved.
Show resolved Hide resolved

[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);
}
}
jeffhandley marked this conversation as resolved.
Show resolved Hide resolved

[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();
}
}
jeffhandley marked this conversation as resolved.
Show resolved Hide resolved
}
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));
jeffhandley marked this conversation as resolved.
Show resolved Hide resolved
}

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));
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
}

if (useBuffer)
Expand Down