diff --git a/contracts/utils/structs/CircularBuffer.sol b/contracts/utils/structs/CircularBuffer.sol index ae4e082595a..1d0adf8196b 100644 --- a/contracts/utils/structs/CircularBuffer.sol +++ b/contracts/utils/structs/CircularBuffer.sol @@ -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. @@ -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); } diff --git a/test/utils/structs/CircularBuffer.test.js b/test/utils/structs/CircularBuffer.test.js index c3d61aaa823..e79ba6923b4 100644 --- a/test/utils/structs/CircularBuffer.test.js +++ b/test/utils/structs/CircularBuffer.test.js @@ -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);