Skip to content

Commit 11b5d42

Browse files
pjt33MrJul
andauthored
Fix SlicedStream.Seek(offset, SeekOrigin.End) (#18313)
* Fix SlicedStream.Seek(offset, SeekOrigin.End) This fixes #13604 The offset _from is applied in set_Position, so applying it also in Seek mispositions the stream. ZipArchive exposes the problem by seeking to the end to read the table of contents. * Add tests for SlicedStream --------- Co-authored-by: Julien Lebosquain <[email protected]>
1 parent 72caa3c commit 11b5d42

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Avalonia.Base/Platform/Internal/SlicedStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override long Seek(long offset, SeekOrigin origin)
2929
if (origin == SeekOrigin.Begin)
3030
Position = offset;
3131
if (origin == SeekOrigin.End)
32-
Position = _from + Length + offset;
32+
Position = Length + offset;
3333
if (origin == SeekOrigin.Current)
3434
Position = Position + offset;
3535
return Position;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.IO;
2+
using Avalonia.Platform.Internal;
3+
using Xunit;
4+
5+
namespace Avalonia.Base.UnitTests;
6+
7+
public class SlicedStreamTests
8+
{
9+
[Theory]
10+
[InlineData(2, SeekOrigin.Begin, 22, 2, 9)]
11+
[InlineData(2, SeekOrigin.Current, 22, 17, 24)]
12+
[InlineData(-2, SeekOrigin.End, 22, 40, 47)]
13+
public void Seek_Works(
14+
long offset,
15+
SeekOrigin origin,
16+
long startingUnderlyingPosition,
17+
long expectedPosition,
18+
long expectedUnderlyingPosition)
19+
{
20+
var memoryStream = new MemoryStream(new byte[1024]);
21+
var slicedStream = new SlicedStream(memoryStream, 7, 42);
22+
memoryStream.Position = startingUnderlyingPosition;
23+
24+
slicedStream.Seek(offset, origin);
25+
26+
Assert.Equal(expectedPosition, slicedStream.Position);
27+
Assert.Equal(expectedUnderlyingPosition, memoryStream.Position);
28+
}
29+
}

0 commit comments

Comments
 (0)