In Solidity, people use a SafeMath library to prevent overflow/underflow attack. So, I wonder what's the best way to prevent overflow/underflow attack for EOS contract. Is there an easy-to-use SafeMath library available?
1 Answer
EOS uses C ++, and C ++ assert can be used to check for overflow or underflow.
ex) assert( quantity + a.quantity >= a.quantity,"integer overflow adding token balance" );
See the link below for this.
https://steemit.com/eos/@dan/eos-example-exchange-contract-and-benefits-of-c
Also, eosio provides eosio_assert to enable various assertions.
-
1That check only works for unsigned types. Optimizing compilers often strip out that check on signed types since ISO C and ISO C++ make signed overflow undefined behavior. Asset uses signed. Commented Jun 24, 2018 at 20:31
-
1AFAIK
assert()
is a macro and not active in the production compiled code, instead it is preprocessed out. So this would not prevent attack. Commented Jun 25, 2018 at 9:22