Skip to content
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
43 changes: 43 additions & 0 deletions src/libraries/System.IO.FileSystem/tests/FileStream/Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,48 @@ public void NegativeReadRootThrows()
Assert.Throws<UnauthorizedAccessException>(() =>
new FileStream(Path.GetPathRoot(Directory.GetCurrentDirectory()), FileMode.Open, FileAccess.Read));
}

[Fact]
public void NoInt32OverflowInTheBufferingLogic()
{
const long positon1 = 10;
const long positon2 = (1L << 32) + positon1;

string filePath = GetTestFilePath();
byte[] data1 = new byte[] { 1, 2, 3, 4, 5 };
byte[] data2 = new byte[] { 6, 7, 8, 9, 10 };
byte[] buffer = new byte[5];

using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
stream.Seek(positon1, SeekOrigin.Begin);
stream.Write(data1, 0, data1.Length);

stream.Seek(positon2, SeekOrigin.Begin);
stream.Write(data2, 0, data2.Length);
}

using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
stream.Seek(positon1, SeekOrigin.Begin);
Assert.Equal(buffer.Length, stream.Read(buffer));
Assert.Equal(data1, buffer);

stream.Seek(positon2, SeekOrigin.Begin);
Assert.Equal(buffer.Length, stream.Read(buffer));
Assert.Equal(data2, buffer);
}

using (var stream = new BufferedStream(new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize: 0)))
{
stream.Seek(positon1, SeekOrigin.Begin);
Assert.Equal(buffer.Length, stream.Read(buffer));
Assert.Equal(data1, buffer);

stream.Seek(positon2, SeekOrigin.Begin);
Assert.Equal(buffer.Length, stream.Read(buffer));
Assert.Equal(data2, buffer);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1222,11 +1222,12 @@ public override long Seek(long offset, SeekOrigin origin)
// Otherwise we will throw away the buffer. This can only happen on read, as we flushed write data above.

// The offset of the new/updated seek pointer within _buffer:
_readPos = (int)(newPos - (oldPos - _readPos));
long readPos = (newPos - (oldPos - _readPos));

// If the offset of the updated seek pointer in the buffer is still legal, then we can keep using the buffer:
if (0 <= _readPos && _readPos < _readLen)
if (0 <= readPos && readPos < _readLen)
{
_readPos = (int)readPos;
// Adjust the seek pointer of the underlying stream to reflect the amount of useful bytes in the read buffer:
_stream.Seek(_readLen - _readPos, SeekOrigin.Current);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -974,11 +974,12 @@ public override long Seek(long offset, SeekOrigin origin)
// Otherwise we will throw away the buffer. This can only happen on read, as we flushed write data above.

// The offset of the new/updated seek pointer within _buffer:
_readPos = (int)(newPos - (oldPos - _readPos));
long readPos = (newPos - (oldPos - _readPos));

// If the offset of the updated seek pointer in the buffer is still legal, then we can keep using the buffer:
if (0 <= _readPos && _readPos < _readLen)
if (0 <= readPos && readPos < _readLen)
{
_readPos = (int)readPos;
// Adjust the seek pointer of the underlying stream to reflect the amount of useful bytes in the read buffer:
_strategy.Seek(_readLen - _readPos, SeekOrigin.Current);
}
Expand Down