Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -1078,7 +1078,8 @@ private async ValueTask WriteToUnderlyingStreamAsync(
{
// 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
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ public void WriteFromSpan_InputSizeLargerThanHalfOfMaxInt_ShouldSuccess()
}
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.Is64BitProcess))]
[OuterLoop]
public async Task WriteAsyncFromMemory_InputSizeLargerThanHalfOfMaxInt_ShouldSuccess()
{
const int InputSize = int.MaxValue / 2 + 1;
byte[] bytes;
try
{
bytes = new byte[InputSize];
}
catch (OutOfMemoryException)
{
throw new SkipTestException("Not enough memory");
}

var writableStream = new WriteOnlyStream();
using (var bs = new BufferedStream(writableStream))
{
await bs.WriteAsync(new ReadOnlyMemory<byte>(bytes));
Assert.Equal(InputSize, writableStream.Position);
}
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down
Loading