Solidity
潛在的重入漏洞:我應該如何重新安排這個功能來避免它?
我不知道為什麼我不斷收到有關“publishEtherBox”功能的潛在重入攻擊漏洞。合約程式碼不完整,但應該足夠理解。這是一個糟糕的語法問題還是其他問題?
contract { ... struct EtherBox { bytes32 label; address owner; string ownerUrl; uint256 expiration; } mapping (address => bytes32) public nicknames; mapping (address => address[]) public ownerToEtherBoxes; mapping (address => EtherBox) public etherBoxes; mapping (address => uint256) etherBoxesNonce; ... function publishEtherBox (bytes32 _label, string _ownerUrl, uint _lifespan) external onlyWhenRunning() payable { require(ownerToEtherBoxes[msg.sender].length < 10); assert(bytes(_ownerUrl).length <= 200); address etherBoxAddress = address(keccak256(msg.sender, etherBoxesNonce[msg.sender]++)); ownerToEtherBoxes[msg.sender].push(etherBoxAddress); etherBoxes[etherBoxAddress] = EtherBox({ label: _label, owner: msg.sender, ownerUrl: _ownerUrl, expiration: now + _lifespan }); if(msg.value > 0){ owner.transfer(msg.value); } emit EtherBoxPublished(msg.sender, nicknames[msg.sender], etherBoxAddress, _label, now); } ... }
這裡沒有真正的問題,但是如果您想使編譯器警告靜音,您可以將
.transfer()
呼叫作為函式中的最後一件事:function publishEtherBox(...) ... { ... emit EtherBoxPublished(msg.sender, nicknames[msg.sender], etherBoxAddress, _label, now); if (msg.value > 0) { owner.transfer(msg.value); } }