Skip to content

Commit

Permalink
Fixing each corner case for the cyclic buffer overflow (#1605)
Browse files Browse the repository at this point in the history
Co-authored-by: Pierre Wielders <pierre@wielders.net>
  • Loading branch information
VeithMetro and pwielders authored May 23, 2024
1 parent 63876be commit 9cd25af
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions Source/core/CyclicBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,25 +394,25 @@ namespace Core {
foundData = false;
}
} else {
uint32_t part1 = 0;
uint32_t part2 = 0;
uint32_t newOffset = 0;

if (_administration->_size < offset) {
part2 = result - (offset - _administration->_size);
const uint32_t skip = offset - _administration->_size;
newOffset = skip + bufferLength;
::memcpy(buffer, _realBuffer + skip, bufferLength);
} else {
part1 = _administration->_size - offset;
part2 = result - part1;
}

memcpy(buffer, _realBuffer + offset, std::min(part1, bufferLength));
const uint32_t part1 = _administration->_size - offset;
newOffset = result - part1;
::memcpy(buffer, _realBuffer + offset, std::min(part1, bufferLength));

if (part1 < bufferLength) {
memcpy(buffer + part1, _realBuffer, bufferLength - part1);
if (part1 < bufferLength) {
::memcpy(buffer + part1, _realBuffer, bufferLength - part1);
}
}

// Add one round, but prevent overflow.
roundCount = (roundCount + 1) % _administration->_roundCountModulo;
uint32_t newTail = part2 + roundCount * (1 + _administration->_tailIndexMask);
uint32_t newTail = newOffset + roundCount * (1 + _administration->_tailIndexMask);
if (!_administration->_tail.compare_exchange_weak(oldTail, newTail)) {
foundData = false;
}
Expand Down

0 comments on commit 9cd25af

Please sign in to comment.