Skip to content

Commit

Permalink
Fix double-free when returning pooled buffers (#8508)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenBond authored Jun 23, 2023
1 parent 91cf648 commit f93d7d3
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Orleans.Serialization/Buffers/PooledBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void Advance(int bytes)
public void Reset()
{
var current = _first;
while (current != null)
while (current != null && current != _writeHead)
{
var previous = current;
current = previous.Next as SequenceSegment;
Expand Down Expand Up @@ -354,16 +354,14 @@ private void Commit()
if (_first is null)
{
_first = _writeHead;
_last = _writeHead;
}
else
{

Debug.Assert(_last is not null);
_last.SetNext(_writeHead);
_last = _writeHead;
}

_last = _writeHead;
_writeHead = null;
_currentPosition = 0;
}
Expand Down Expand Up @@ -509,7 +507,7 @@ public bool MoveNext()
{
if (ReferenceEquals(_segment, InitialSegmentSentinel))
{
_segment = _slice._buffer._first;
_segment = Buffer._first;
}

var endPosition = Offset + Length;
Expand Down Expand Up @@ -553,6 +551,9 @@ public bool MoveNext()
return true;
}

// Account for the uncommitted data at the end of the buffer.
// The write head is only linked to the previous buffers when Commit() is called and it is set to null afterwards,
// meaning that if the write head is not null, the other buffers are not linked to it and it therefore has not been enumerated.
if (_segment != FinalSegmentSentinel && Buffer._currentPosition > 0 && Buffer._writeHead is { } head && _position < endPosition)
{
var finalOffset = Math.Max(Offset - _position, 0);
Expand Down

0 comments on commit f93d7d3

Please sign in to comment.