Skip to content

Commit

Permalink
fix: remove unchecked around loops
Browse files Browse the repository at this point in the history
  • Loading branch information
lekhovitsky committed Jul 7, 2024
1 parent 7405c87 commit 138225a
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 128 deletions.
7 changes: 2 additions & 5 deletions contracts/adapters/balancer/BalancerV2VaultAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,8 @@ contract BalancerV2VaultAdapter is AbstractAdapter, IBalancerV2VaultAdapter {
/// @dev Internal function that changes approval for a batch of assets in the vault (overloading)
function _approveAssets(IAsset[] memory assets, uint256[] memory filter, uint256 amount) internal {
uint256 len = assets.length;

unchecked {
for (uint256 i = 0; i < len; ++i) {
if (filter[i] > 1) _approveToken(address(assets[i]), amount);
}
for (uint256 i = 0; i < len; ++i) {
if (filter[i] > 1) _approveToken(address(assets[i]), amount);
}
}

Expand Down
36 changes: 17 additions & 19 deletions contracts/adapters/camelot/CamelotV3Adapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -280,26 +280,24 @@ contract CamelotV3Adapter is AbstractAdapter, ICamelotV3Adapter {
/// @param pools Array of `CamelotV3PoolStatus` objects
function setPoolStatusBatch(CamelotV3PoolStatus[] calldata pools) external override configuratorOnly {
uint256 len = pools.length;
unchecked {
for (uint256 i; i < len; ++i) {
(address token0, address token1) = _sortTokens(pools[i].token0, pools[i].token1);

bytes32 poolHash = keccak256(abi.encode(token0, token1));
if (pools[i].allowed) {
/// For each added pool, we verify that the pool tokens are valid collaterals,
/// as otherwise operations with unsupported tokens would be possible, leading
/// to possibility of control flow capture
_getMaskOrRevert(token0);
_getMaskOrRevert(token1);

_supportedPoolHashes.add(poolHash);
_hashToPool[poolHash] = CamelotV3Pool({token0: token0, token1: token1});
} else {
_supportedPoolHashes.remove(poolHash);
delete _hashToPool[poolHash];
}
emit SetPoolStatus(token0, token1, pools[i].allowed); // U: [CAMV3-9]
for (uint256 i; i < len; ++i) {
(address token0, address token1) = _sortTokens(pools[i].token0, pools[i].token1);

bytes32 poolHash = keccak256(abi.encode(token0, token1));
if (pools[i].allowed) {
/// For each added pool, we verify that the pool tokens are valid collaterals,
/// as otherwise operations with unsupported tokens would be possible, leading
/// to possibility of control flow capture
_getMaskOrRevert(token0);
_getMaskOrRevert(token1);

_supportedPoolHashes.add(poolHash);
_hashToPool[poolHash] = CamelotV3Pool({token0: token0, token1: token1});
} else {
_supportedPoolHashes.remove(poolHash);
delete _hashToPool[poolHash];
}
emit SetPoolStatus(token0, token1, pools[i].allowed); // U: [CAMV3-9]
}
}

Expand Down
42 changes: 20 additions & 22 deletions contracts/adapters/convex/ConvexV1_Booster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -180,28 +180,26 @@ contract ConvexV1BoosterAdapter is AbstractAdapter, IConvexV1BoosterAdapter {

address[] memory allowedAdapters = cc.allowedAdapters();
uint256 len = allowedAdapters.length;
unchecked {
for (uint256 i = 0; i < len; ++i) {
address adapter = allowedAdapters[i];
address poolTargetContract = IAdapter(adapter).targetContract();

if (
IAdapter(adapter).contractType() == "AD_CONVEX_V1_BASE_REWARD_POOL"
&& IBaseRewardPool(poolTargetContract).operator() == targetContract
) {
uint256 pid = IBaseRewardPool(poolTargetContract).pid();
address phantomToken = IConvexV1BaseRewardPoolAdapter(adapter).stakedPhantomToken();

/// No sanity checks on pool-related tokens (Curve token, Convex token, phantom token) being collateral
/// need to be performed, as they were already done while deploying the pool adapter itself

pidToPhantomToken[pid] = phantomToken;
pidToCurveToken[pid] = IConvexV1BaseRewardPoolAdapter(adapter).curveLPtoken();
pidToConvexToken[pid] = IConvexV1BaseRewardPoolAdapter(adapter).stakingToken();

_supportedPids.add(pid);
emit AddSupportedPid(pid);
}
for (uint256 i = 0; i < len; ++i) {
address adapter = allowedAdapters[i];
address poolTargetContract = IAdapter(adapter).targetContract();

if (
IAdapter(adapter).contractType() == "AD_CONVEX_V1_BASE_REWARD_POOL"
&& IBaseRewardPool(poolTargetContract).operator() == targetContract
) {
uint256 pid = IBaseRewardPool(poolTargetContract).pid();
address phantomToken = IConvexV1BaseRewardPoolAdapter(adapter).stakedPhantomToken();

/// No sanity checks on pool-related tokens (Curve token, Convex token, phantom token) being collateral
/// need to be performed, as they were already done while deploying the pool adapter itself

pidToPhantomToken[pid] = phantomToken;
pidToCurveToken[pid] = IConvexV1BaseRewardPoolAdapter(adapter).curveLPtoken();
pidToConvexToken[pid] = IConvexV1BaseRewardPoolAdapter(adapter).stakingToken();

_supportedPids.add(pid);
emit AddSupportedPid(pid);
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions contracts/adapters/curve/CurveV1_Base.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,10 @@ abstract contract CurveV1AdapterBase is AbstractAdapter, ICurveV1Adapter {
use256 = _use256();

address[4] memory tokens;
unchecked {
for (uint256 i; i < nCoins; ++i) {
tokens[i] = _getCoin(_curvePool, i); // U:[CRVB-1]
if (tokens[i] == address(0)) revert IncorrectParameterException(); // U:[CRVB-1]
_getMaskOrRevert(tokens[i]); // U:[CRVB-1]
}
for (uint256 i; i < nCoins; ++i) {
tokens[i] = _getCoin(_curvePool, i); // U:[CRVB-1]
if (tokens[i] == address(0)) revert IncorrectParameterException(); // U:[CRVB-1]
_getMaskOrRevert(tokens[i]); // U:[CRVB-1]
}

token0 = tokens[0];
Expand Down
34 changes: 16 additions & 18 deletions contracts/adapters/uniswap/UniswapV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -181,25 +181,23 @@ contract UniswapV2Adapter is AbstractAdapter, IUniswapV2Adapter {
configuratorOnly // U:[UNI2-6]
{
uint256 len = pairs.length;
unchecked {
for (uint256 i; i < len; ++i) {
(address token0, address token1) = _sortTokens(pairs[i].token0, pairs[i].token1);
bytes32 pairHash = keccak256(abi.encode(token0, token1));
if (pairs[i].allowed) {
/// For each added pool, we verify that the pool tokens are valid collaterals,
/// as otherwise operations with unsupported tokens would be possible, leading
/// to possibility of control flow capture
_getMaskOrRevert(token0);
_getMaskOrRevert(token1);

_supportedPairHashes.add(pairHash);
_hashToPair[pairHash] = UniswapV2Pair({token0: token0, token1: token1});
} else {
_supportedPairHashes.remove(pairHash);
delete _hashToPair[pairHash];
}
emit SetPairStatus(token0, token1, pairs[i].allowed); // U:[UNI2-6]
for (uint256 i; i < len; ++i) {
(address token0, address token1) = _sortTokens(pairs[i].token0, pairs[i].token1);
bytes32 pairHash = keccak256(abi.encode(token0, token1));
if (pairs[i].allowed) {
/// For each added pool, we verify that the pool tokens are valid collaterals,
/// as otherwise operations with unsupported tokens would be possible, leading
/// to possibility of control flow capture
_getMaskOrRevert(token0);
_getMaskOrRevert(token1);

_supportedPairHashes.add(pairHash);
_hashToPair[pairHash] = UniswapV2Pair({token0: token0, token1: token1});
} else {
_supportedPairHashes.remove(pairHash);
delete _hashToPair[pairHash];
}
emit SetPairStatus(token0, token1, pairs[i].allowed); // U:[UNI2-6]
}
}

Expand Down
34 changes: 16 additions & 18 deletions contracts/adapters/uniswap/UniswapV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -241,25 +241,23 @@ contract UniswapV3Adapter is AbstractAdapter, IUniswapV3Adapter {
configuratorOnly // U:[UNI3-9]
{
uint256 len = pools.length;
unchecked {
for (uint256 i; i < len; ++i) {
(address token0, address token1) = _sortTokens(pools[i].token0, pools[i].token1);
bytes32 poolHash = keccak256(abi.encode(token0, token1, pools[i].fee));
if (pools[i].allowed) {
/// For each added pool, we verify that the pool tokens are valid collaterals,
/// as otherwise operations with unsupported tokens would be possible, leading
/// to possibility of control flow capture
_getMaskOrRevert(token0);
_getMaskOrRevert(token1);

_supportedPoolHashes.add(poolHash);
_hashToPool[poolHash] = UniswapV3Pool({token0: token0, token1: token1, fee: pools[i].fee});
} else {
_supportedPoolHashes.remove(poolHash);
delete _hashToPool[poolHash];
}
emit SetPoolStatus(token0, token1, pools[i].fee, pools[i].allowed); // U:[UNI3-9]
for (uint256 i; i < len; ++i) {
(address token0, address token1) = _sortTokens(pools[i].token0, pools[i].token1);
bytes32 poolHash = keccak256(abi.encode(token0, token1, pools[i].fee));
if (pools[i].allowed) {
/// For each added pool, we verify that the pool tokens are valid collaterals,
/// as otherwise operations with unsupported tokens would be possible, leading
/// to possibility of control flow capture
_getMaskOrRevert(token0);
_getMaskOrRevert(token1);

_supportedPoolHashes.add(poolHash);
_hashToPool[poolHash] = UniswapV3Pool({token0: token0, token1: token1, fee: pools[i].fee});
} else {
_supportedPoolHashes.remove(poolHash);
delete _hashToPool[poolHash];
}
emit SetPoolStatus(token0, token1, pools[i].fee, pools[i].allowed); // U:[UNI3-9]
}
}

Expand Down
44 changes: 21 additions & 23 deletions contracts/adapters/velodrome/VelodromeV2RouterAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -151,30 +151,28 @@ contract VelodromeV2RouterAdapter is AbstractAdapter, IVelodromeV2RouterAdapter
/// @param pools Array of `VelodromeV2PoolStatus` objects
function setPoolStatusBatch(VelodromeV2PoolStatus[] calldata pools) external override configuratorOnly {
uint256 len = pools.length;
unchecked {
for (uint256 i; i < len; ++i) {
(address token0, address token1) = _sortTokens(pools[i].token0, pools[i].token1);
bytes32 poolHash = keccak256(abi.encode(token0, token1, pools[i].stable, pools[i].factory));
if (pools[i].allowed) {
/// For each added pool, we verify that the pool tokens are valid collaterals,
/// as otherwise operations with unsupported tokens would be possible, leading
/// to possibility of control flow capture
_getMaskOrRevert(token0);
_getMaskOrRevert(token1);

_supportedPoolHashes.add(poolHash);
_hashToPool[poolHash] = VelodromeV2Pool({
token0: token0,
token1: token1,
stable: pools[i].stable,
factory: pools[i].factory
});
} else {
_supportedPoolHashes.remove(poolHash);
delete _hashToPool[poolHash];
}
emit SetPoolStatus(token0, token1, pools[i].stable, pools[i].factory, pools[i].allowed); // U: [VELO2-05]
for (uint256 i; i < len; ++i) {
(address token0, address token1) = _sortTokens(pools[i].token0, pools[i].token1);
bytes32 poolHash = keccak256(abi.encode(token0, token1, pools[i].stable, pools[i].factory));
if (pools[i].allowed) {
/// For each added pool, we verify that the pool tokens are valid collaterals,
/// as otherwise operations with unsupported tokens would be possible, leading
/// to possibility of control flow capture
_getMaskOrRevert(token0);
_getMaskOrRevert(token1);

_supportedPoolHashes.add(poolHash);
_hashToPool[poolHash] = VelodromeV2Pool({
token0: token0,
token1: token1,
stable: pools[i].stable,
factory: pools[i].factory
});
} else {
_supportedPoolHashes.remove(poolHash);
delete _hashToPool[poolHash];
}
emit SetPoolStatus(token0, token1, pools[i].stable, pools[i].factory, pools[i].allowed); // U: [VELO2-05]
}
}

Expand Down
31 changes: 14 additions & 17 deletions contracts/adapters/zircuit/ZircuitPoolAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,20 @@ contract ZircuitPoolAdapter is AbstractAdapter, IZircuitPoolAdapter {
ICreditManagerV3 cm = ICreditManagerV3(creditManager);

uint256 len = cm.collateralTokensCount();

unchecked {
for (uint256 i = 0; i < len; ++i) {
address token = cm.getTokenByMask(1 << i);
try IPhantomToken(token)._gearboxPhantomTokenType() returns (PhantomTokenType ptType) {
if (ptType == PhantomTokenType.ZIRCUIT_PHANTOM_TOKEN) {
address depositedToken = ZircuitPhantomToken(token).underlying();

_getMaskOrRevert(token);
_getMaskOrRevert(depositedToken);

tokenToPhantomToken[depositedToken] = token;
_supportedUnderlyings.add(depositedToken);
emit AddSupportedUnderlying(depositedToken, token);
}
} catch {}
}
for (uint256 i = 0; i < len; ++i) {
address token = cm.getTokenByMask(1 << i);
try IPhantomToken(token)._gearboxPhantomTokenType() returns (PhantomTokenType ptType) {
if (ptType == PhantomTokenType.ZIRCUIT_PHANTOM_TOKEN) {
address depositedToken = ZircuitPhantomToken(token).underlying();

_getMaskOrRevert(token);
_getMaskOrRevert(depositedToken);

tokenToPhantomToken[depositedToken] = token;
_supportedUnderlyings.add(depositedToken);
emit AddSupportedUnderlying(depositedToken, token);
}
} catch {}
}
}
}

0 comments on commit 138225a

Please sign in to comment.