4

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

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.

|improve this answer|||||
  • 1
    That 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. – Todd Fleming Jun 24 '18 at 20:31
  • 1
    AFAIK assert() is a macro and not active in the production compiled code, instead it is preprocessed out. So this would not prevent attack. – Mikko Ohtamaa Jun 25 '18 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.