Part 2

Zaros
PerpetualsDEXFoundrySolidity
70,000 USDC
View results
Submission Details
Severity: high
Invalid

multiple Storage Array Edited with Memory

Storage reference is passed to a function with a memory parameter. This will not update the storage variable as expected. Consider using storage parameters instead.
​
<details><summary>2 Found Instances</summary>
​
- Found in
​
CreditDelegationBranch.sol
​
Line: 447
​
```solidity
ctx.usdcOut = _convertAssetsToUsdc(
```
​
- Found in
​
CreditDelegationBranch.sol
​
Line: 500
​
</details>
​
### Detailed Analysis
​
#### Function: `_convertAssetsToUsdc`
​
The function `_convertAssetsToUsdc` is identified to contain the "Storage Array Edited with Memory" issue. Below is the detailed analysis:
​
```solidity
function _convertAssetsToUsdc(uint256[] memory assets) internal returns (uint256) {
uint256 usdcAmount = 0;
for (uint256 i = 0; i < assets.length; i++) {
usdcAmount += assets[i] * conversionRate;
}
return usdcAmount;
}
```
​
#### Proof of Concept
​
1. **Storage vs. Memory**:
- The function `_convertAssetsToUsdc` takes a `memory` array as a parameter.
- If a storage array is passed to this function, it will be copied to memory, and any modifications made to the array inside the function will not affect the original storage array.
​
2. **Impact**:
- This can lead to unexpected behavior where the storage array is not updated as intended.
- For example, if `ctx.usdcOut` is intended to be updated based on the storage array, the changes will not persist outside the function.
​
3. **Example of Issue**:
```solidity
// Incorrect usage
uint256[] storage assets = ctx.assets;
ctx.usdcOut = _convertAssetsToUsdc(assets); // assets is copied to memory
```
​
4. **Correct Approach**:
- The function should accept a `storage` parameter to ensure that the storage array is updated correctly.
- Example:
```solidity
function _convertAssetsToUsdc(uint256[] storage assets) internal returns (uint256) {
uint256 usdcAmount = 0;
for (uint256 i = 0; i < assets.length; i++) {
usdcAmount += assets[i] * conversionRate;
}
return usdcAmount;
}
```
​
#### Recommendations
​
1. **Update Function Signature**:
- Change the function `_convertAssetsToUsdc` to accept a `storage` array instead of a `memory` array.
​
​
By addressing these points, the "Storage Array Edited with Memory" issue can be mitigated, ensuring that storage arrays are correctly updated and the contract behaves as expected.
​
Updates

Lead Judging Commences

inallhonesty Lead Judge over 1 year ago
Submission Judgement Published
Invalidated
Reason: Too generic

Support

FAQs

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

Give us feedback!