Commit e70dd187 by Dan Phifer

Moving business logic preconditions to the beginning of the function.

parent 158a7a88
...@@ -21,6 +21,7 @@ contract BasicToken is ERC20Basic { ...@@ -21,6 +21,7 @@ contract BasicToken is ERC20Basic {
*/ */
function transfer(address _to, uint256 _value) public returns (bool) { function transfer(address _to, uint256 _value) public returns (bool) {
require(_to != address(0)); require(_to != address(0));
require(_value <= balances[msg.sender]);
// SafeMath.sub will throw if there is not enough balance. // SafeMath.sub will throw if there is not enough balance.
balances[msg.sender] = balances[msg.sender].sub(_value); balances[msg.sender] = balances[msg.sender].sub(_value);
......
...@@ -16,6 +16,9 @@ contract BurnableToken is StandardToken { ...@@ -16,6 +16,9 @@ contract BurnableToken is StandardToken {
*/ */
function burn(uint256 _value) public { function burn(uint256 _value) public {
require(_value > 0); 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; address burner = msg.sender;
balances[burner] = balances[burner].sub(_value); balances[burner] = balances[burner].sub(_value);
......
...@@ -25,15 +25,12 @@ contract StandardToken is ERC20, BasicToken { ...@@ -25,15 +25,12 @@ contract StandardToken is ERC20, BasicToken {
*/ */
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) { function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
require(_to != address(0)); require(_to != address(0));
require(_value <= balances[_from]);
uint256 _allowance = allowed[_from][msg.sender]; require(_value <= allowed[_from][msg.sender]);
// Check is not needed because sub(_allowance, _value) will already throw if this condition is not met
// require (_value <= _allowance);
balances[_from] = balances[_from].sub(_value); balances[_from] = balances[_from].sub(_value);
balances[_to] = balances[_to].add(_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); Transfer(_from, _to, _value);
return true; return true;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment