Beatland Festival

AI First Flight #4
Beginner FriendlyFoundrySolidityNFT
EXP
View results
Submission Details
Severity: medium
Valid

Off-by-one in redeemMemorabilia — the last item of every collection can never be redeemed; a maxSupply=1 collection is fully bricked

Root + Impact

Description

redeemMemorabilia() has an off-by-one error in its sold-out check. Item IDs start at 1, but the check is currentItemId < maxSupply:

@> require(collection.currentItemId < collection.maxSupply, "Collection sold out");

currentItemId starts at 1 (set in createMemorabiliaCollection), and each redemption mints currentItemId then increments it. So for a collection with maxSupply = 3, items 1 and 2 redeem fine, but after the second redemption currentItemId becomes 3 and 3 < 3 is false — the third item can never be redeemed. Only maxSupply - 1 items can ever be minted from any collection.

Worse, a collection with maxSupply = 1 is completely bricked from creation: the very first redemption checks 1 < 1, which is false, so zero items can ever be redeemed from it even though the organizer configured it to sell one item.

Risk

Likelihood: High — the bug triggers deterministically on every collection; the last item is always unreachable, and a maxSupply=1 collection fails on its very first redemption.

Impact: Medium — the organizer's configured supply is wrong by one item in every collection (and entirely unusable for maxSupply=1), so collectors cannot redeem what was advertised and the memorabilia economy is short one item per collection.

Proof of Concept

A maxSupply=3 collection only ever redeems items 1 and 2; the third redemption reverts "Collection sold out" even though only 2 of 3 items were sold. A maxSupply=1 collection reverts on its first redemption.

function test_M1_offByOne_lastItemNeverRedeemable() public {
uint256 collectionId = _createCollection(3, 10e18);
​
vm.prank(alice);
festivalPass.buyPass{value: BACKSTAGE_PRICE}(3);
assertEq(beatToken.balanceOf(alice), 15e18);
​
vm.prank(alice);
festivalPass.redeemMemorabilia(collectionId);
assertEq(festivalPass.tokenIdToEdition(festivalPass.encodeTokenId(collectionId, 1)), 1);
​
vm.prank(alice);
festivalPass.buyPass{value: BACKSTAGE_PRICE}(3); // +15 BEAT
vm.prank(alice);
festivalPass.redeemMemorabilia(collectionId);
assertEq(festivalPass.tokenIdToEdition(festivalPass.encodeTokenId(collectionId, 2)), 2);
​
// Only 2 of 3 items redeemed, yet the 3rd can NEVER be redeemed:
// currentItemId is now 3, and 3 < 3 is false.
vm.prank(alice);
festivalPass.buyPass{value: BACKSTAGE_PRICE}(3); // +15 BEAT, plenty
vm.prank(alice);
vm.expectRevert("Collection sold out");
festivalPass.redeemMemorabilia(collectionId);
}
​
function test_M1_offByOne_maxSupplyOneIsFullyBricked() public {
uint256 collectionId = _createCollection(1, 10e18);
​
vm.prank(alice);
festivalPass.buyPass{value: BACKSTAGE_PRICE}(3);
assertEq(beatToken.balanceOf(alice), 15e18);
​
// currentItemId starts at 1, and 1 < 1 is false: zero items
// can ever be redeemed from this collection.
vm.prank(alice);
vm.expectRevert("Collection sold out");
festivalPass.redeemMemorabilia(collectionId);
}

Recommended Mitigation

Use <= so the item numbered maxSupply is reachable.

- require(collection.currentItemId < collection.maxSupply, "Collection sold out");
+ require(collection.currentItemId <= collection.maxSupply, "Collection sold out");
Updates

Lead Judging Commences

ai-first-flight-judge Lead Judge 29 minutes ago
Submission Judgement Published
Validated
Assigned finding tags:

[M-03] Off-by-One in `redeemMemorabilia` Prevents Last NFT From Being Redeemed

# Off-by-One in `redeemMemorabilia` Prevents Last NFT From Being Redeemed ## Description * The `createMemorabiliaCollection` function allows an organizer to create an NFT collection that can be exchanged for the BEAT token via the `redeemMemorabilia` function by users. * The `redeemMemorabilia` function checks if `collection.currentItemId` is less than `collection.maxSupply`. However, the `currentItemId` starts with 1 in the `createMemorabiliaCollection` function. This prevents the final item (where `currentItemId` equals `maxSupply`) from being redeemed. ```Solidity function createMemorabiliaCollection( string memory name, string memory baseUri, uint256 priceInBeat, uint256 maxSupply, bool activateNow ) external onlyOrganizer returns (uint256) { require(priceInBeat > 0, "Price must be greater than 0"); require(maxSupply > 0, "Supply must be at least 1"); require(bytes(name).length > 0, "Name required"); require(bytes(baseUri).length > 0, "URI required"); uint256 collectionId = nextCollectionId++; collections[collectionId] = MemorabiliaCollection({ name: name, baseUri: baseUri, priceInBeat: priceInBeat, maxSupply: maxSupply, @> currentItemId: 1, // Start item IDs at 1 isActive: activateNow }); emit CollectionCreated(collectionId, name, maxSupply); return collectionId; } function redeemMemorabilia(uint256 collectionId) external { MemorabiliaCollection storage collection = collections[collectionId]; require(collection.priceInBeat > 0, "Collection does not exist"); require(collection.isActive, "Collection not active"); @> require(collection.currentItemId < collection.maxSupply, "Collection sold out"); // Burn BEAT tokens BeatToken(beatToken).burnFrom(msg.sender, collection.priceInBeat); // Generate unique token ID uint256 itemId = collection.currentItemId++; uint256 tokenId = encodeTokenId(collectionId, itemId); // Store edition number tokenIdToEdition[tokenId] = itemId; // Mint the unique NFT _mint(msg.sender, tokenId, 1, ""); emit MemorabiliaRedeemed(msg.sender, tokenId, collectionId, itemId); } ``` ## Risk **Likelihood**: * A legitimate user calls `redeemMemorabilia` attempting to redeem the last NFT in a collection. **Impact**: * The user fails to get the NFT, even though the redemption counter has not reached the maximum supply of the collection. ## Proof of Concept The following test shows a user trying to redeem the 10th NFT in one collection. Running `forge test --mt test_Audit_RedeemMaxSupply -vv` shows the output that the 10th redemption is reverted due to the sold out. ```Solidity function test_Audit_RedeemMaxSupply() public { vm.prank(organizer); uint256 maxSupply = 10; // Cap for memorabilia NFT collection uint256 collectionId = festivalPass.createMemorabiliaCollection( "Future Release", "ipfs://QmFuture", 10e18, maxSupply, true ); vm.startPrank(address(festivalPass)); beatToken.mint(user1, 10000e18); // Give enough BEAT for user vm.stopPrank(); vm.startPrank(user1); for (uint256 i = 0; i < maxSupply - 1; i++) { festivalPass.redeemMemorabilia(collectionId); console.log("Redeem sucess:", i + 1); // Redeem success from 1 to 9 } // 10th redeem call reverts with "Collection Sold out" vm.expectRevert("Collection sold out"); festivalPass.redeemMemorabilia(collectionId); console.log("Redeem reverted:", maxSupply); vm.stopPrank(); } ``` ## Recommended Mitigation Modify the supply check in `redeemMemorabilia` to use `<=` (less than or equal to) instead of `<`, ensuring that the final item can be redeemed. This approach is preferable to modifying the `createMemorabiliaCollection` function (which is clearly documented to start `currentItemId` at 1). ```diff // Redeem a memorabilia NFT from a collection function redeemMemorabilia(uint256 collectionId) external { MemorabiliaCollection storage collection = collections[collectionId]; require(collection.priceInBeat > 0, "Collection does not exist"); require(collection.isActive, "Collection not active"); - require(collection.currentItemId < collection.maxSupply, "Collection sold out"); + require(collection.currentItemId <= collection.maxSupply, "Collection sold out"); // allow equals // Burn BEAT tokens BeatToken(beatToken).burnFrom(msg.sender, collection.priceInBeat); // Generate unique token ID uint256 itemId = collection.currentItemId++; uint256 tokenId = encodeTokenId(collectionId, itemId); // Store edition number tokenIdToEdition[tokenId] = itemId; // Mint the unique NFT _mint(msg.sender, tokenId, 1, ""); emit MemorabiliaRedeemed(msg.sender, tokenId, collectionId, itemId); } ```

Support

FAQs

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

Give us feedback!