From e70dd187342f83a4c447a950dfbdb0f1ca32ef35 Mon Sep 17 00:00:00 2001 From: Dan Phifer Date: Sat, 23 Sep 2017 11:57:29 +0800 Subject: [PATCH] Moving business logic preconditions to the beginning of the function. --- contracts/token/BasicToken.sol | 1 + contracts/token/BurnableToken.sol | 3 +++ contracts/token/StandardToken.sol | 9 +++------ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/contracts/token/BasicToken.sol b/contracts/token/BasicToken.sol index 7df250013..a45157f81 100644 --- a/contracts/token/BasicToken.sol +++ b/contracts/token/BasicToken.sol @@ -21,6 +21,7 @@ contract BasicToken is ERC20Basic { */ function transfer(address _to, uint256 _value) public returns (bool) { require(_to != address(0)); + require(_value <= balances[msg.sender]); // SafeMath.sub will throw if there is not enough balance. balances[msg.sender] = balances[msg.sender].sub(_value); diff --git a/contracts/token/BurnableToken.sol b/contracts/token/BurnableToken.sol index 67cb850fe..c155bd188 100644 --- a/contracts/token/BurnableToken.sol +++ b/contracts/token/BurnableToken.sol @@ -16,6 +16,9 @@ contract BurnableToken is StandardToken { */ function burn(uint256 _value) public { require(_value > 0); + require(_value <= balances[msg.sender]); + // no need to require value <= totalSupply, since that would imply the + // sender's balance is greater than the totalSupply, which *should* be an assertion failure address burner = msg.sender; balances[burner] = balances[burner].sub(_value); diff --git a/contracts/token/StandardToken.sol b/contracts/token/StandardToken.sol index 061c4c958..7ab917c95 100644 --- a/contracts/token/StandardToken.sol +++ b/contracts/token/StandardToken.sol @@ -25,15 +25,12 @@ contract StandardToken is ERC20, BasicToken { */ function transferFrom(address _from, address _to, uint256 _value) public returns (bool) { require(_to != address(0)); - - uint256 _allowance = allowed[_from][msg.sender]; - - // Check is not needed because sub(_allowance, _value) will already throw if this condition is not met - // require (_value <= _allowance); + require(_value <= balances[_from]); + require(_value <= allowed[_from][msg.sender]); balances[_from] = balances[_from].sub(_value); balances[_to] = balances[_to].add(_value); - allowed[_from][msg.sender] = _allowance.sub(_value); + allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value); Transfer(_from, _to, _value); return true; }