Beatland Festival

First Flight #44
Beginner FriendlyFoundrySolidityNFT
100 EXP
View results
Submission Details
Impact: low
Likelihood: high
Invalid

Unchecked Increment in buyPass Can Save Gas

Root + Impact

Description

  • Normal Behavior:
    The buyPass function is called every time a user purchases a festival pass. It increments the passSupply[collectionId] variable to track the number of passes sold for each collection. Solidity increments are checked by default, which incurs a small but non-negligible gas cost.

    Issue:
    In this function, the increment is always safe because the function already checks that passSupply[collectionId] < passMaxSupply[collectionId] before incrementing. This means an overflow is impossible. Using a checked increment here is unnecessary and results in higher gas costs for every pass purchase. Wrapping the increment in an unchecked block can save gas, especially when the function is called frequently.

function buyPass(uint256 collectionId) external payable {
// ...existing code...
require(passSupply[collectionId] < passMaxSupply[collectionId], "Max supply reached");
_mint(msg.sender, collectionId, 1, "");
++passSupply[collectionId]; // Can be unchecked
// ...existing code...
}

Risk

Likelihood:

  • This code path is executed on every pass purchase, and the inefficiency is multiplied by the number of purchases.

Impact:

  • The gas savings are minor per call, but can add up with many purchases and are especially relevant for high-volume mints. Over time, this can result in significant cost savings for users and the protocol.

Proof of Concept

The increment is always safe due to the preceding require. This means overflow is impossible, so the increment can be safely wrapped in an unchecked block to save gas.

require(passSupply[collectionId] < passMaxSupply[collectionId], "Max supply reached");

Recommended Mitigation

Wrap the increment in an unchecked block to reduce gas usage in buyPass.

- ++passSupply[collectionId];
+ unchecked { ++passSupply[collectionId]; }
Updates

Lead Judging Commences

inallhonesty Lead Judge 26 days ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.