Skip to content

Commit

Permalink
ReadOnlySequence: Don't leak the flags that are on _startInteger and …
Browse files Browse the repository at this point in the history
…_endInteger. (#47969)

* ReadOnlySequence: Don't leak the flags that are on _startInteger and _endInteger.

* Typo fix.

* Added more tests + review fixes.
  • Loading branch information
Kuinox authored Mar 17, 2021
1 parent 0f64b26 commit 29dbd32
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ internal SequencePosition Seek(long offset, ExceptionArgument exceptionArgument
int currentLength = startSegment.Memory.Length - startIndex;

// Position in start segment, defer to single segment seek
if (currentLength > offset)
if (currentLength > offset || offset == 0)
goto IsSingleSegment;

if (currentLength < 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public bool IsSingleSegment
public SequencePosition Start
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new SequencePosition(_startObject, _startInteger);
get => new SequencePosition(_startObject, GetIndex(_startInteger));
}

/// <summary>
Expand All @@ -71,7 +71,7 @@ public SequencePosition Start
public SequencePosition End
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new SequencePosition(_endObject, _endInteger);
get => new SequencePosition(_endObject, GetIndex(_endInteger));
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -660,6 +660,10 @@ private enum SequenceType

internal static class ReadOnlySequence
{
/// <summary>
/// Flag that allows encoding the <see cref="ReadOnlySequence{T}.SequenceType"/>.
/// </summary>
/// <seealso cref="ReadOnlySequence{T}.GetSequenceType"/>
public const int FlagBitMask = 1 << 31;
public const int IndexBitMask = ~FlagBitMask;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,36 @@ public void CopyTo_ThrowsWhenSourceLargerThenDestination()
});
}

[Fact]
public void End_EqualToGetPositionSize()
{
ReadOnlySequence<byte> buffer = Factory.CreateOfSize(5);
Assert.Equal(buffer.End, buffer.GetPosition(5));
}

[Fact]
public void Start_EqualToGetPosition0()
{
ReadOnlySequence<byte> buffer = Factory.CreateOfSize(5);
Assert.Equal(buffer.Start, buffer.GetPosition(0));
}

[Fact]
public void InnerPositionAreNotEqualToEnd()
{
ReadOnlySequence<byte> buffer = Factory.CreateOfSize(3);
Assert.NotEqual(buffer.GetPosition(1), buffer.End);
Assert.NotEqual(buffer.GetPosition(2), buffer.End);
}

[Fact]
public void InnerPositionAreNotEqualToStart()
{
ReadOnlySequence<byte> buffer = Factory.CreateOfSize(3);
Assert.NotEqual(buffer.GetPosition(1), buffer.Start);
Assert.NotEqual(buffer.GetPosition(2), buffer.Start);
}

public static TheoryData<Func<ReadOnlySequence<byte>, ReadOnlySequence<byte>>> ValidSliceCases => new TheoryData<Func<ReadOnlySequence<byte>, ReadOnlySequence<byte>>>
{
b => b.Slice(5),
Expand Down

0 comments on commit 29dbd32

Please sign in to comment.