# Incorrect index handling in checkpoint creation leads to incorrect initial checkpoint retrieval and potential DoS

Flayer ProtectedListings._createCheckpoint returns wrong index when updating same-timestamp checkpoint, causing listings to reference nonexistent checkpoint and DoS unlockPrice.

- Payout: Audit contest
- Severity: high
- Program: Flayer
- Found by: 0x37
- Disclosed: 2024-08-06
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-08-flayer-judging/issues/732
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-flayer%3AH-24
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:dos

**Target:** ProtectedListings contract (Solidity, ERC721 listing checkpoints)
**Root cause:** _createCheckpoint returns index_ (array length) even when it only updates the existing checkpoint at index_-1 without pushing a new one, so callers store an out-of-range index.
**Steps:**
1. Call createListings with two CreateListing entries for the same collection that has zero existing checkpoints.
2. First iteration: _createCheckpoint returns 0, tstore stores 0; listing mapped to checkpoint 0.
3. Second iteration: tload returns 0 (sentinel), so _createCheckpoint runs again; same timestamp path updates checkpoint[0] but returns index_ = 1.
4. Listing is mapped to checkpoint index 1 which does not exist (array length 1).
5. unlockPrice reads collectionCheckpoints[_collection][listing.checkpoint] with index 1 -> out-of-bounds revert, DoS.
**How found:** Code reading of _createCheckpoint's same-timestamp branch returning index_ instead of index_-1, combined with createListings' transient-storage sentinel of 0 causing re-invocation.
**Exploitation:** Attacker creates multiple listings for a fresh collection in one createListings call; second listing gets checkpoint index 1 while only index 0 exists. Subsequent unlockPrice for that listing reverts, blocking unlock and causing DoS.
**Insight:** When a function returns an array length as a 'new index' but has an early-return path that only updates an existing element, the returned index is off-by-one; also watch transient-storage sentinels where 0 is a valid index.

## Proof of concept (copied exactly from the original report)

**createListings transient checkpoint index logic**

```
checkpointKey = keccak256(abi.encodePacked('checkpointIndex', listing.collection));
135:             assembly { checkpointIndex := tload(checkpointKey) }
136:             if (checkpointIndex == 0) {
137:                 checkpointIndex = _createCheckpoint(listing.collection);
138:                 assembly { tstore(checkpointKey, checkpointIndex) }
139:             }
---
143:             tokensReceived = _mapListings(listing, tokensIdsLength, checkpointIndex) * 10 ** locker.collectionToken(listing.collection).denomination();
```

**_createCheckpoint returns wrong index on same-timestamp update**

```
if (checkpoint.timestamp == collectionCheckpoints[_collection][index_ - 1].timestamp) {
565:@>             collectionCheckpoints[_collection][index_ - 1].compoundedFactor = checkpoint.compoundedFactor;
566:@>             return index_;
```

**unlockPrice indexes checkpoints by listing.checkpoint**

```
unlockPrice_ = locker.taxCalculator().compound({
613:             _principle: listing.tokenTaken,
614:             _initialCheckpoint: collectionCheckpoints[_collection][listing.checkpoint],
615:             _currentCheckpoint: _currentCheckpoint(_collection)
```

