Skip to content
Open
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 @@ -334,7 +334,7 @@ public override int Read(byte[] buffer, int offset, int count)
if (_superStream.Position != _positionInSuperStream)
_superStream.Seek(_positionInSuperStream, SeekOrigin.Begin);
if (_positionInSuperStream + count > _endInSuperStream)
count = (int)(_endInSuperStream - _positionInSuperStream);
count = (int)Math.Max(0L, _endInSuperStream - _positionInSuperStream);

Debug.Assert(count >= 0);
Debug.Assert(count <= origCount);
Expand All @@ -357,7 +357,7 @@ public override int Read(Span<byte> destination)
if (_superStream.Position != _positionInSuperStream)
_superStream.Seek(_positionInSuperStream, SeekOrigin.Begin);
if (_positionInSuperStream + count > _endInSuperStream)
count = (int)(_endInSuperStream - _positionInSuperStream);
count = (int)Math.Max(0L, _endInSuperStream - _positionInSuperStream);

Debug.Assert(count >= 0);
Debug.Assert(count <= origCount);
Expand Down Expand Up @@ -395,7 +395,7 @@ async ValueTask<int> Core(Memory<byte> buffer, CancellationToken cancellationTok

if (_positionInSuperStream > _endInSuperStream - buffer.Length)
{
buffer = buffer.Slice(0, (int)(_endInSuperStream - _positionInSuperStream));
buffer = buffer.Slice(0, (int)Math.Max(0L, _endInSuperStream - _positionInSuperStream));
}

int ret = await _superStream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,5 +889,35 @@ public static async Task CompressionMethod_Deflate64_ReturnsDeflate64(bool async
Assert.Equal(ZipCompressionMethod.Deflate64, readEntry.CompressionMethod);
await DisposeZipArchive(async, readArchive);
}

[Theory]
[MemberData(nameof(Get_Booleans_Data))]
public static async Task ReadAfterSeekingPastEnd_ReturnsZeroBytes(bool async)
{
using var ms = new MemoryStream();
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, leaveOpen: true))
{
var entry = archive.CreateEntry("test.txt", CompressionLevel.NoCompression);
using var stream = entry.Open();
stream.Write("Hello, World!"u8);
}

ms.Position = 0;
using var readArchive = await CreateZipArchive(async, ms, ZipArchiveMode.Read);
Stream readStream = await OpenEntryStream(async, readArchive.Entries[0]);

readStream.Seek(1, SeekOrigin.End);
Assert.Equal(14, readStream.Position);

byte[] buffer = new byte[1024];
int bytesRead = async
? await readStream.ReadAsync(buffer)
: readStream.Read(buffer, 0, buffer.Length);

Assert.Equal(0, bytesRead);
Assert.Equal(14, readStream.Position);

await DisposeStream(async, readStream);
}
}
}
Loading