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 3 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
100 changes: 100 additions & 0 deletions src/libraries/System.IO/tests/BufferedStream/BufferedStreamTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace System.IO.Tests
{
public class BufferedStream_StreamAsync
{
public static bool IsX64 { get; } = IntPtr.Size >= 8;
Copy link
Member

@stephentoub stephentoub Jun 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't necessary... you can use this (as a theory):

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.Is64BitProcess))]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, thanks!


[Fact]
public static void NullConstructor_Throws_ArgumentNullException()
{
Expand Down Expand Up @@ -53,6 +55,54 @@ public void BufferSize()
Assert.Equal(1234, bufferedStream.BufferSize);
}

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

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

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down Expand Up @@ -369,4 +419,54 @@ 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 - offset);
}

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 => throw new NotSupportedException();
set => throw new NotSupportedException();
}

public long GetPosition()
{
return _pos;
}
}
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