Summary
Constructor is missing a zero address check. This could result in unexpected behavior when attempting to use the contract.
Vulnerability Details
The constructor is missing a zero address check for the decentralisation stable coin address and can be initialised without an error prompting the user.
constructor(
address[] memory tokenAddresses,
address[] memory priceFeedAddresses,
address dscAddress
) {
if (tokenAddresses.length != priceFeedAddresses.length) {
revert DSCEngine__TokenAddressesAndPriceFeedAddressesMustBeSameLength();
}
for (uint256 i = 0; i < tokenAddresses.length; i++) {
s_priceFeeds[tokenAddresses[i]] = priceFeedAddresses[i];
s_collateralTokens.push(tokenAddresses[i]);
}
i_dsc = DecentralizedStableCoin(dscAddress);
}
Impact
DSCEngine contract initialised with zero address for decentralised stablecoin address
TEST
function test_DSZeroAddress() public {
tokenAddresses.push(weth);
feedAddresses.push(btcUsdPriceFeed);
dsce = new DSCEngine(tokenAddresses, feedAddresses, address(0x0));
address dsceAddr = dsce.getDsc();
emit log_named_address("Decentralised Stablecoin address", dsceAddr);
}
RESULT
Running 1 test for test/unit/DSCEngineTest.t.sol:DSCEngineTest
[PASS] test_DSZeroAddress() (gas: 1093878)
Logs:
Decentralised Stablecoin address: 0x0000000000000000000000000000000000000000
Test result: ok. 1 passed; 0 failed; finished in 3.38ms
Tools Used
Manual review, Foundry test
Recommendations
Implement a zero address check using the require function and the != (inequality) operator with address(0).
Example:
constructor(
address[] memory tokenAddresses,
address[] memory priceFeedAddresses,
address dscAddress
) {
require(dscAddress != address(0), "Invalid decentralised stablecoin address");
if (tokenAddresses.length != priceFeedAddresses.length) {
revert DSCEngine__TokenAddressesAndPriceFeedAddressesMustBeSameLength();
}
for (uint256 i = 0; i < tokenAddresses.length; i++) {
s_priceFeeds[tokenAddresses[i]] = priceFeedAddresses[i];
s_collateralTokens.push(tokenAddresses[i]);
}
i_dsc = DecentralizedStableCoin(dscAddress);
}