浏览代码

Disallow empty CircularBuffer setup (#5214)

Ernesto García 1 年之前
父节点
当前提交
530179a71f
共有 2 个文件被更改,包括 10 次插入0 次删除
  1. 6 0
      contracts/utils/structs/CircularBuffer.sol
  2. 4 0
      test/utils/structs/CircularBuffer.test.js

+ 6 - 0
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);
     }

+ 4 - 0
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);