Disallow empty CircularBuffer setup (#5214)

pull/5215/head
Ernesto García 5 months ago committed by GitHub
parent b1f6bbe69f
commit 530179a71f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      contracts/utils/structs/CircularBuffer.sol
  2. 4
      test/utils/structs/CircularBuffer.test.js

@ -36,6 +36,11 @@ import {Panic} from "../Panic.sol";
* ``` * ```
*/ */
library CircularBuffer { 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 * @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. * where the next value should be stored.
@ -61,6 +66,7 @@ library CircularBuffer {
* Consider a large buffer size may render the function unusable. * Consider a large buffer size may render the function unusable.
*/ */
function setup(Bytes32CircularBuffer storage self, uint256 size) internal { function setup(Bytes32CircularBuffer storage self, uint256 size) internal {
if (size == 0) revert InvalidBufferSize();
clear(self); clear(self);
Arrays.unsafeSetLength(self._data, size); Arrays.unsafeSetLength(self._data, size);
} }

@ -18,6 +18,10 @@ describe('CircularBuffer', function () {
Object.assign(this, await loadFixture(fixture)); 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 () { it('starts empty', async function () {
expect(await this.mock.$count(0)).to.equal(0n); expect(await this.mock.$count(0)).to.equal(0n);
expect(await this.mock.$length(0)).to.equal(LENGTH); expect(await this.mock.$length(0)).to.equal(LENGTH);

Loading…
Cancel
Save