-
Notifications
You must be signed in to change notification settings - Fork 242
SafeERC20: add extra returndatasize check to staticInvoke() #540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: next
Are you sure you want to change the base?
Changes from all commits
4641762
fbf0fad
4feb97a
696e701
ec1e0f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -75,7 +75,16 @@ library SafeERC20 { | |
| ) | ||
|
|
||
| if gt(success, 0) { | ||
| ret := mload(ptr) | ||
| switch returndatasize | ||
|
|
||
| // 32 bytes returned; is valid return | ||
| case 0x20 { | ||
| ret := mload(ptr) | ||
| } | ||
| // Else, mark call as failed | ||
| default { | ||
| success := 0 | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that we upgraded
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately there's no |
||
| } | ||
| } | ||
| return (success, ret); | ||
|
|
@@ -123,7 +132,7 @@ library SafeERC20 { | |
|
|
||
| /** | ||
| * @dev Static call into ERC20.balanceOf(). | ||
| * Reverts if the call fails for some reason (should never fail). | ||
| * Reverts if the call fails for some reason (should never fail for correctly implemented ERC20s). | ||
| */ | ||
| function staticBalanceOf(ERC20 _token, address _owner) internal view returns (uint256) { | ||
| bytes memory balanceOfCallData = abi.encodeWithSelector( | ||
|
|
@@ -139,7 +148,7 @@ library SafeERC20 { | |
|
|
||
| /** | ||
| * @dev Static call into ERC20.allowance(). | ||
| * Reverts if the call fails for some reason (should never fail). | ||
| * Reverts if the call fails for some reason (should never fail for correctly implemented ERC20s). | ||
| */ | ||
| function staticAllowance(ERC20 _token, address _owner, address _spender) internal view returns (uint256) { | ||
| bytes memory allowanceCallData = abi.encodeWithSelector( | ||
|
|
@@ -153,4 +162,17 @@ library SafeERC20 { | |
|
|
||
| return allowance; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Static call into ERC20.totalSupply(). | ||
| * Reverts if the call fails for some reason (should never fail for correctly implemented ERC20s). | ||
| */ | ||
| function staticTotalSupply(ERC20 _token) internal view returns (uint256) { | ||
| bytes memory totalSupplyCallData = abi.encodeWithSelector(_token.totalSupply.selector); | ||
|
|
||
| (bool success, uint256 totalSupply) = staticInvoke(_token, totalSupplyCallData); | ||
| require(success, ERROR_TOKEN_ALLOWANCE_REVERTED); | ||
|
|
||
| return totalSupply; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // Non-standards compliant token that is missing return values for | ||
| // `allowance()` and `balanceOf()`. | ||
| // Modified from https://github.com/OpenZeppelin/openzeppelin-solidity/blob/a9f910d34f0ab33a1ae5e714f69f9596a02b4d91/contracts/token/ERC20/StandardToken.sol | ||
|
|
||
| pragma solidity 0.4.24; | ||
|
|
||
| import "../../../../lib/math/SafeMath.sol"; | ||
|
|
||
|
|
||
| contract TokenBalanceOfAllowanceReturnMissingMock { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WDYT about modeling this case within
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking about that... but what stopped me was in the |
||
| using SafeMath for uint256; | ||
| mapping (address => uint256) private balances; | ||
| mapping (address => mapping (address => uint256)) private allowed; | ||
| uint256 private totalSupply_; | ||
| bool private allowTransfer_; | ||
|
|
||
| event Approval(address indexed owner, address indexed spender, uint256 value); | ||
| event Transfer(address indexed from, address indexed to, uint256 value); | ||
|
|
||
| // Allow us to set the inital balance for an account on construction | ||
| constructor(address initialAccount, uint256 initialBalance) public { | ||
| balances[initialAccount] = initialBalance; | ||
| totalSupply_ = initialBalance; | ||
| allowTransfer_ = true; | ||
| } | ||
|
|
||
| function totalSupply() public view returns (uint256) { return totalSupply_; } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that I'm thinking of... shouldn't we provide a static version of
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JIC #543
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense; we didn't have a use case for it before but it doesn't hurt to include it :). |
||
|
|
||
| /** | ||
| * @dev Gets the balance of the specified address. | ||
| * @param _owner The address to query the the balance of. | ||
| * @return An uint256 representing the amount owned by the passed address. | ||
| */ | ||
| function balanceOf(address _owner) public view { | ||
| } | ||
|
|
||
| /** | ||
| * @dev Function to check the amount of tokens that an owner allowed to a spender. | ||
| * @param _owner address The address which owns the funds. | ||
| * @param _spender address The address which will spend the funds. | ||
| * @return A uint256 specifying the amount of tokens still available for the spender. | ||
| */ | ||
| function allowance(address _owner, address _spender) public view { | ||
| } | ||
|
|
||
| /** | ||
| * @dev Set whether the token is transferable or not | ||
| * @param _allowTransfer Should token be transferable | ||
| */ | ||
| function setAllowTransfer(bool _allowTransfer) public { | ||
| allowTransfer_ = _allowTransfer; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Transfer token for a specified address | ||
| * @param _to The address to transfer to. | ||
| * @param _value The amount to be transferred. | ||
| */ | ||
| function transfer(address _to, uint256 _value) public returns (bool) { | ||
| require(allowTransfer_); | ||
| require(_value <= balances[msg.sender]); | ||
| require(_to != address(0)); | ||
|
|
||
| balances[msg.sender] = balances[msg.sender].sub(_value); | ||
| balances[_to] = balances[_to].add(_value); | ||
| emit Transfer(msg.sender, _to, _value); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender. | ||
| * Beware that changing an allowance with this method brings the risk that someone may use both the old | ||
| * and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this | ||
| * race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards: | ||
| * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 | ||
| * @param _spender The address which will spend the funds. | ||
| * @param _value The amount of tokens to be spent. | ||
| */ | ||
| function approve(address _spender, uint256 _value) public returns (bool) { | ||
| // Assume we want to protect for the race condition | ||
| require(allowed[msg.sender][_spender] == 0); | ||
|
|
||
| allowed[msg.sender][_spender] = _value; | ||
| emit Approval(msg.sender, _spender, _value); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Transfer tokens from one address to another | ||
| * @param _from address The address which you want to send tokens from | ||
| * @param _to address The address which you want to transfer to | ||
| * @param _value uint256 the amount of tokens to be transferred | ||
| */ | ||
| function transferFrom(address _from, address _to, uint256 _value) public returns (bool) { | ||
| require(allowTransfer_); | ||
| require(_value <= balances[_from]); | ||
| require(_value <= allowed[_from][msg.sender]); | ||
| require(_to != address(0)); | ||
|
|
||
| balances[_from] = balances[_from].sub(_value); | ||
| balances[_to] = balances[_to].add(_value); | ||
| allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value); | ||
| emit Transfer(_from, _to, _value); | ||
| return true; | ||
| } | ||
| } | ||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: The revert reasons in
staticBalanceOfandstaticAllowancedenote that the call to the token reverted, which is no longer always the case with this change. I don't think we should change the revert reason but we should at least make a note for this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment in the function header says "Reverts if the call fails for some reason (should never fail)", we could just update it here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modified to "(should never fail for correctly implemented ERC20s)"