So I want to write as simple as possible withdrawal function in a token contract which also doesn't use Safemath for one very simple reason: I admit I don't understand well enough data types in EVM so I'll rather stick to basic logical statements to prevent hacking, so how safe is this:
function withdrawTokens(uint _amount) external returns (uint _available) {
_available = _amount < balances[msg.sender] ? _amount : balances[msg.sender];
msg.sender.transfer(_available);
balances[msg.sender] -= _available;
emit eventWithd(msg.sender, _available);
}
My major concern is this line:
balances[msg.sender] -= _available;
This will obviously underflow if the balance of the msg.sender is say 5 and they send 6 as argument (5-6 = -1 but in uint it's huge positive number...), so I am betting on the fact that at least one of these 2 lines will revert thus never reaching the dangerous line above:
_available = _amount < balances[msg.sender] ? _amount balances[msg.sender];
msg.sender.transfer(_available);
Any thoughts? Thanx a lot!