# MovingAverage.setSampleMemory() may broke MovingAverage, making the value of exchangeRate in StabilizerNode.stabilize() being extremely wrong

Admin setSampleMemory() resizes the MovingAverage ring buffer without reindexing samples, so getValueWithLookback() returns near-zero or reverts, corrupting StabilizerNode exchangeRate.

- Payout: Audit contest
- Severity: medium
- Program: Malt Finance
- Found by: WatchPug
- Disclosed: 2022-02-14
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2021-11-malt-findings/issues/313
- Library page: https://bugbountylibrary.com/r/c4%3A2021-11-malt%3AM-07
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:solidity technique:source-review vuln:accounting-error vuln:oracle-manipulation

**Target:** MovingAverage.sol ring-buffer oracle used by StabilizerNode
**Root cause:** setSampleMemory pushes empty samples and applies counter % _sampleMemory without reindexing existing samples; the shrink branch only sets activeSamples, leaving stale indices. getValueWithLookback then reads wrong/empty slots.
**Steps:**
1. Deploy with sampleMemory=10; call movingAverage.update(1e18) 120 times so all samples hold 1e18.
2. Admin calls movingAverage.setSampleMemory(118).
3. Call movingAverage.getValueWithLookback(sampleLength*10); observe ~0.00000203312e18 instead of 1e18.
4. Further calls may return 0 or revert FullMath: FULLDIV_OVERFLOW at L134.
**How found:** Code review of setSampleMemory: noticed the shrink branch has a TODO and only sets activeSamples, and the grow branch pushes empty samples and mods counter, implying index misalignment with existing samples.
**Exploitation:** Requires ADMIN_ROLE to call setSampleMemory; once misconfigured, StabilizerNode.stabilize() consumes a corrupted exchangeRate, enabling value extraction/mispricing.
**Insight:** When auditing ring-buffer/oracle contracts, check every admin resize path for reindexing of existing entries and counter modulo consistency; a TODO comment in a branch is a strong signal of unhandled state.
