SafeMath doesn't prevent overflow/underflow from user input.
Example:
function sub(uint8 a, uint8 b) public returns(uint8) {
require(b <= a);
uint8 c = a - b;
return c;
}
Say the user inputs a == 30
and b == 260
, it will pass, since by the time the scope of the function is executed, b has already been overflown to 4.
Is there no way of preventing that, other than preventing that input on the client side?
My point is that SafeMath is not nearly safe when you are dealing with user input. But checking that on the client side isn't safe either, since it's always possible to interact with a contract "directly", bypassing any front-end application.
What am I missing here?
by the time the scope of the function is executed
- what does that mean??? The function is executed as a single transaction, start to end.b overflow to 4
? If anything, it would bec
, sinceb
is "untouched". Did you perhaps think that the value ofb
might change on the side of the caller? If yes, then please note thatb
is a local variable here (a copy of whatever value was sent from the other side, if you will).