Open code423n4 opened 3 years ago
Maybe I'm not technical enough to understand this report but I ran some quick tests in remix and found some issues here:
eq()
in the assembly statement which I don't believe is correct, that would imply an AND between result and tmp.It's not clear to me from his example how his version would short circuit the comparison when it matches.
pragma solidity ^0.7.0;
contract Test {
function getImpl1(bytes4 sig) external pure returns (bool) {
bool result = bytes4(0x00000001) == sig;
bool tmp = sig == 0x00000002;
assembly { result := or(result, tmp) }
return result;
}
function getImpl2(bytes4 sig) external pure returns (bool) {
if (sig == 0x00000001 || sig == 0x00000002) {
return true;
}
return false;
}
}
I run similar test and this optimization does not improve gas for me. The difference is very tiny though. Making this invalid.
Handle
hrkrshnn
Vulnerability details
Consider using assembly instead of the lengthy if statement in Router.sol
Consider the function:
there are several large if statements. Take for example:
In the above snippet, there are 10 logical ORs. Because solidity performs short circuiting for the logical or, effectively, this is same as performing 9 if statements, and therefore more expensive than required. Each
if
statement adds at least 20 gas (from 2jumpi
operations alone.)This can be avoided by directly using inline assembly. Here's a rough implementation:
Since
getRouterImplementation
is supposed to mimic Solidity's own function dispatch, it is worthwhile to optimize this function.