Skip to content

Commit

Permalink
Disallow empty CircularBuffer setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestognw committed Sep 19, 2024
1 parent 3f90169 commit 3eb56a6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions contracts/utils/structs/CircularBuffer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ import {Panic} from "../Panic.sol";
* ```
*/
library CircularBuffer {
/**
* @dev Error emitted when trying to setup a buffer with a size of 0.
*/
error InvalidBufferSize();

/**
* @dev Counts the number of items that have been pushed to the buffer. The residuo modulo _data.length indicates
* where the next value should be stored.
Expand All @@ -61,6 +66,7 @@ library CircularBuffer {
* Consider a large buffer size may render the function unusable.
*/
function setup(Bytes32CircularBuffer storage self, uint256 size) internal {
if (size == 0) revert InvalidBufferSize();
clear(self);
Arrays.unsafeSetLength(self._data, size);
}
Expand Down
4 changes: 4 additions & 0 deletions test/utils/structs/CircularBuffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ describe('CircularBuffer', function () {
Object.assign(this, await loadFixture(fixture));
});

it('reverts on invalid setup', async function () {
await expect(this.mock.$setup(0, 0)).to.be.revertedWithCustomError(this.mock, 'InvalidBufferSize');
});

it('starts empty', async function () {
expect(await this.mock.$count(0)).to.equal(0n);
expect(await this.mock.$length(0)).to.equal(LENGTH);
Expand Down

0 comments on commit 3eb56a6

Please sign in to comment.