如果我将要求(这)从传递函数中删除,以节省汽油,并将其保留到用户没有足够数量时产生的错误,它会以任何不好的方式影响函数吗?
function transfer(address recipient, uint256 amount) external returns (bool) {
require(_balances[msg.sender] >= amount)
_balances[msg.sender] -= amount;
_balances[recipient] += amount;
return true;
}
function transfer(address recipient, uint256 amount) external returns (bool) {
_balances[msg.sender] -= amount;
_balances[recipient] += amount;
return true;
}发布于 2021-03-09 19:57:17
这取决于您的可靠版本和balances的数据类型。
我假设balances是mapping (address => uint256),因为这是大多数令牌所使用的。至于可靠的版本,我将把答案分开。
固体<= 0.7.6
不移除check. (如果删除),则打开该函数到整数下溢/溢出漏洞。
示例:
_balances[msg.sender]是50amount是100所以50-100通常会导致-50。由于uint中没有负数,所以它环绕在零附近,并从数据类型中可用的最大数目继续进行子字符串操作。在uint256的情况下,最大值是2^256 - 1 (大约)。10^77)。
如果没有检查,_balances[msg.sender] -= amount;就会下流,导致发送方没有-50而是2^256 - 51令牌。你不想要的。
了解有关https://swcregistry.io/docs/SWC-101漏洞的更多信息
编辑:有一个名为SafeMath (由OpenZeppelin编写)的著名库,它也可以帮助您防止下溢/溢出。示例使用:
// reverts if underflow would happen
_balances[msg.sender] = _balances[msg.sender].sub(amount);稳健性>= 0.8.0
把支票拿走是安全的。Solidity 0.8+在默认情况下执行下溢/溢出检查,如果会发生下溢/溢出,事务将恢复。
https://stackoverflow.com/questions/66549501
复制相似问题