Skip to content

Commit 530179a

Browse files
authored
Disallow empty CircularBuffer setup (#5214)
1 parent b1f6bbe commit 530179a

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

contracts/utils/structs/CircularBuffer.sol

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ import {Panic} from "../Panic.sol";
3636
* ```
3737
*/
3838
library CircularBuffer {
39+
/**
40+
* @dev Error emitted when trying to setup a buffer with a size of 0.
41+
*/
42+
error InvalidBufferSize();
43+
3944
/**
4045
* @dev Counts the number of items that have been pushed to the buffer. The residuo modulo _data.length indicates
4146
* where the next value should be stored.
@@ -61,6 +66,7 @@ library CircularBuffer {
6166
* Consider a large buffer size may render the function unusable.
6267
*/
6368
function setup(Bytes32CircularBuffer storage self, uint256 size) internal {
69+
if (size == 0) revert InvalidBufferSize();
6470
clear(self);
6571
Arrays.unsafeSetLength(self._data, size);
6672
}

test/utils/structs/CircularBuffer.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ describe('CircularBuffer', function () {
1818
Object.assign(this, await loadFixture(fixture));
1919
});
2020

21+
it('reverts on invalid setup', async function () {
22+
await expect(this.mock.$setup(0, 0)).to.be.revertedWithCustomError(this.mock, 'InvalidBufferSize');
23+
});
24+
2125
it('starts empty', async function () {
2226
expect(await this.mock.$count(0)).to.equal(0n);
2327
expect(await this.mock.$length(0)).to.equal(LENGTH);

0 commit comments

Comments
 (0)