Summary
TokenFactory.sol is missing a check for reverted transactions when a new token is being deployed.
Vulnerability Details
Without the iszero() check, the contract would not revert if a token deployment fails, and the s_tokenToAddress variable would still hold a non-zero address even if the deployment was unsuccessful.
Impact
This could lead to incorrect information being stored in the address mapping and potentially cause issues in the contract's functionality.
Tools Used
VSCode
Recommendations
Add the following check to ensure create() would revert if the deployment fails:
function deployToken(bytes memory bytecode, string memory symbol) external onlyOwner returns (address addr) {
assembly {
addr := create(0, add(bytecode, 0x20), mload(bytecode))
+ if iszero(extcodesize(addr)) {
+ revert(0, 0)
+ }
}
s_tokenToAddress[symbol] = addr;
emit TokenDeployed(symbol, addr);
}
Additionally, the following unit test can be added:
function testFailedTokenDeploymentShouldRevert() public {
vm.prank(owner);
bytes memory bytecode = hex"123456";
vm.expectRevert(bytes("EvmError: Revert"));
address tokenAddress = tokenFactory.deployToken("FAIL", bytecode);
}
Running 1 test for test/TokenFactoryTest.t.sol:TokenFactoryTest
[PASS] testFailedTokenDeploymentShouldRevert() (gas: 8937393460516734631)
Traces:
[8937393460516734631] TokenFactoryTest::testFailedTokenDeploymentShouldRevert()
├─ [0] VM::prank(owner: [0x7c8999dC9a822c1f0Df42023113EDB4FDd543266])
│ └─ ← ()
├─ [0] VM::expectRevert(EvmError: Revert)
│ └─ ← ()
├─ [8937393460516723495] TokenFactory::deployToken(FAIL, 0x123456)
│ ├─ [3] → new <Unknown>@0xf801f3A6F4e09F82D6008505C67a0A5b39842406
│ │ └─ ← "EvmError: StackUnderflow"
│ ├─ emit TokenDeployed(symbol: FAIL, addr: 0x0000000000000000000000000000000000000000)
│ └─ ← 0x0000000000000000000000000000000000000000
└─ ← "call did not revert as expected"
Test result: ok. 1 passed; 0 failed; 0 skipped; finished in 1.56ms